响应消息的内容类型application/xml;charset=utf-8 与绑定的内容类型(text/xml;charset=utf-8)不匹配,WCF
Posted
技术标签:
【中文标题】响应消息的内容类型application/xml;charset=utf-8 与绑定的内容类型(text/xml;charset=utf-8)不匹配,WCF【英文标题】:The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8), WCF 【发布时间】:2015-03-02 13:06:53 【问题描述】:我知道这个问题已经被问过几次了,但是给出的解决方案要么对我不起作用,要么我太无知,无法完全理解如何实施该解决方案。
因此,我尝试运行的网站是通过 WCF 客户端运行的。每当尝试调用 WCF 服务(即任何数据库搜索或编辑)时,都会出现此错误
内容类型text/html;响应消息的 charset=utf-8 确实 与绑定的内容类型不匹配 (text/xml; charset=utf-8)。 如果使用自定义编码器,请确保 IsContentTypeSupported 方法实施得当。响应的前 1024 个字节 是:'
当我通过未处理的异常发生此错误时,会弹出以下内容
http://i.imgur.com/Vj7Baik.png
(注意:最后截断的那一行是)
[ProtocolException: The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: '<!DOCTYPE html>
这是用于该站点的 web.config 文件(我不确定问题是否集中在此处,但我有一个令人讨厌的怀疑)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=DefaultConnection;Integrated Security=false;User ID=******;Password=******;"/>
<add name="ITPDatabase" connectionString="Data Source=localhost;Initial Catalog=ITPDatabase;Integrated Security=false;User ID=******;Password=******;"/>
</connectionStrings>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls></pages>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" defaultUrl="~/" timeout="2880" />
</authentication>
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
<providers>
<add connectionStringName="DefaultConnection" applicationName="/"
name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</roleManager>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="defaultEndpointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="*****.Service1" behaviorConfiguration="defaultServiceBehaviour">
<endpoint address="" binding="wsHttpBinding" contract="ServiceReference1.IService1"></endpoint>
<endpoint address="http://<domainname>.com/Service1.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<client>
<endpoint address="http://<domainname>.com/Service1.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<handlers accessPolicy="Read, Execute, Script">
<add name="svc-Integrated" path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" resourceType="File" preCondition="integratedMode" /> </handlers>
</system.webServer>
</configuration>
现在,从我收集的其他问题来看,我收到的错误的要点不是从数据库获取 xml 响应,而是收到了一个 html 页面错误。
在问这里之前,我一直在从网络主机的技术支持那里获得帮助,但此时他们不确定问题是什么(或者至少,我与之交谈的人不确定) 我们使用 IIS 尝试修改系统中的某些设置(没有真正的改变),所以我可以安全地假设该站点托管在 IIS 上?
非常感谢您对此事的任何帮助。
【问题讨论】:
The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8)的可能重复 【参考方案1】:您收到 500 错误。这意味着错误是在托管服务的服务器上引发的,而不是来自调用者。我的猜测是错误的数据被发送到服务,例如 null 或字符串而不是 int,或者没有发送服务所需的所有必需位。
要获得更详细的错误,您必须
a) 在托管服务的服务器上运行相同的请求
例如 - 如果您对服务的调用是 http://www.domain.com/service.svc?id=1,让他们在托管服务的服务器上运行 URL 并向您发送错误。
b) 通过修改服务的 web.config 禁用自定义错误以获取实际错误
<configuration>
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
<system.web>
<customErrors mode="Off" />
<compilation debug="true" />
</system.web>
</configuration>
【讨论】:
您好,感谢您的快速回复。我尝试修改 web.config(以及 webRelease.config 文件以防万一)以更改 customerrors,但是当我尝试通过网站搜索/常规 wcf 功能时会发生相同的错误。但是,我确实尝试运行以上是关于响应消息的内容类型application/xml;charset=utf-8 与绑定的内容类型(text/xml;charset=utf-8)不匹配,WCF的主要内容,如果未能解决你的问题,请参考以下文章
Asp.Net Web API 错误:“ObjectContent`1”类型无法序列化内容类型“application/xml”的响应正文;字符集=utf-8'
“ObjectContent`1”类型未能序列化内容类型“application/xml; charset=utf-8”的响应正文。
C# WebApi Xml序列化问题解决方法:“ObjectContent`1”类型未能序列化内容类型“application/xml;charset=utf-8"的响应正文。...
webapi “ObjectContent`1”类型未能序列化内容类型“application/xml; charset=utf-8”的响应正文。
无法处理该消息,因为内容类型“应用程序/xml”不是预期的类型“应用程序/soap + xml”;字符集 = utf-8 '
Asp.Net Web API错误:'ObjectContent`1'类型无法序列化内容类型'application / xml的响应主体;字符集= UTF-8'(代码