WCF服务客户端:内容typestext / html; 响应消息的charset = utf-8与绑定的内容types不匹配

我有我的本地IIS服务器上运行的WCF服务。 我已经将它作为服务引用添加到C#Website Project中,并且它会自动添加并自动生成代理类。

但是,当我尝试调用任何服务合同时,出现以下错误:

说明:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪,了解有关错误的更多信息以及源代码的来源。

exception详细信息: System.ServiceModel.ProtocolException:内容typestext / html; 响应消息的charset = utf-8与绑定的内容types(application / soap + xml; charset = utf-8)不匹配。 如果使用自定义编码器,请确保IsContentTypeSupported方法正确实施。 响应的前1024个字节是:函数bredir(var,h,wd,hd,bi; var b = false; var p = false; var s = [[ 300250,假],[250250,假],[240400,假],[336280,假],[180150,假],[468,60,假],[234,60,假],[88,31,假],[120,90,假],[120,60,假],[120240,假],[125125,假],[728,90,假],[160600,假],[120600,假] [300600,假],[300125,假],[530300,假],[190200,假],[470250,假],[720300,真],[500350,真],[550480,真]]; if(typeof(window.innerHeight)=='number'){h = window.innerHeight; w = window.innerWidth;} else if(typeof(document.body.offsetHeight)=='number'){h = document。 body.offsetHeight; w = document.body.offsetWidth;} for(var i = 0; i

我也有一个控制台应用程序,它也与WCF服务进行通信,控制台应用程序能够调用方法很好,而不会出现此错误。

以下是我的configuration文件的摘录。

WCF服务Web.Config:

<system.serviceModel> <services> <service name="ScraperService" behaviorConfiguration="ScraperServiceBehavior"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IScraperService" contract="IScraperService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://example.com" /> </baseAddresses> </host> </service> </services> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IScraperService" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="2000000" maxStringContentLength="2000000" maxArrayLength="2000000" maxBytesPerRead="2000000" maxNameTableCharCount="2000000" /> <reliableSession enabled="false" ordered="true" inactivityTimeout="00:10:00" /> <security mode="Message"> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="ScraperServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> 

网站项目服务客户端Web.Config

 <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IScraperService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession enabled="false" ordered="true" inactivityTimeout="00:10:00" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint name="WSHttpBinding_IScraperService" address="http://example.com/ScraperService.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IScraperService" contract="ScraperService.IScraperService" > <identity> <servicePrincipalName value="host/FreshNET-II" /> </identity> </endpoint> </client> </system.serviceModel> 

这是我第一次尝试创build一个WCF,所以这是非常新的。 任何帮助深表感谢。

谢谢。

尝试浏览到托pipe服务的服务器上的Web浏览器中的http://localhost/ScraperService.svc ,使用客户端正常运行的相同Windows凭据。

我想象的是IIS显示一些描述的HTML错误信息,而不是像预期的那样返回XML。

当您有一个执行Internet过滤的http代理服务器时,也会发生这种情况。 我使用ContentKeeper的经验是拦截任何http / httpsstream量,并将其拦截为“非托pipe内容” – 我们所得到的只是一个html错误消息。 为避免这种情况,您可以将代理服务器例外规则添加到Internet Explorer中,以便代理不会拦截到您网站的stream量:

控制面板> Internet选项>连接> LAN设置>高级>代理设置

在这里输入图像描述

来自Web服务器的HTML响应通常表示已经提供了错误页面,而不是来自WCF服务的响应。 我的第一个build议是检查您正在运行的WCF客户端下的用户有权访问该资源。

发生了什么事是你试图使用wsHttpBind访问服务,默认情况下使用安全的encryption消息(安全消息)。 另一方面,netTcpBind使用Securedencryption通道。 (Secured Transport)…但basicHttpBind,根本不需要任何安全性,并且可以匿名访问

所以。 在服务器端添加\将其更改为您的configuration。

 <bindings> <wsHttpBinding> <binding name="wsbind"> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings> 

然后添加更改您的端点

 <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsbind" name="wshttpbind" contract="WCFService.IService" > 

这应该做到这一点。

在我的情况下,一个URL重写规则搞乱我的服务名称,它被重写为小写,我得到这个错误。

确保您不要小写WCF服务调用。

与许多人一样,在我的情况下,我也因为错误而得到这个。 不幸的是,我只能阅读html错误页面的CSS。

我的问题的来源也是服务器上的重写规则。 这是重写http到https。

您可能需要检查您的服务的configuration,并确保一切正常。 您可以通过浏览器导航到Web服务,以查看架构是否将在浏览器上呈现。

您可能还需要检查用于调用服务的凭证。

我尝试了上面的所有build议,但最终发生了什么变化是将应用程序池pipe理的pipe道从集成模式更改为经典模式。
它运行在自己的应用程序池中 – 但是它是第一个.NET 4.0服务 – 所有其他服务都使用集成pipe道模式在.NET 2.0上。 它只是一个标准的WCF服务使用的是https – 但在Server 2008(不是R2) – 使用IIS 7(不是7.5)。

我有类似的情况,但客户端configuration使用basicHttpBinding。 原来这个问题是服务使用SOAP 1.2,你不能在basicHttpBinding中指定SOAP 1.2。 我修改了客户端configuration使用customBinding,而不是一切正常。 这里是我的customBinding作为参考的细节。 我试图使用的服务是通过使用UserNameOverTransport的HTTPS。

 <customBinding> <binding name="myBindingNameHere" sendTimeout="00:03:00"> <security authenticationMode="UserNameOverTransport" includeTimestamp="false"> <secureConversationBootstrap /> </security> <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> </textMessageEncoding> <httpsTransport manualAddressing="false" maxBufferPoolSize="4194304" maxReceivedMessageSize="4194304" allowCookies="false" authenticationScheme="Basic" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="4194304" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" requireClientCertificate="false" /> </binding> </customBinding> 

即使您不使用networking代理,在代理对话框中转动“自动检测设置”也会使此exception消失。

在这里输入图像描述

在我的WCF serive项目中,这个问题是由于引用System.Web.Mvc.dll的不同版本造成的。 所以它可能是DLL的不同版本的兼容性问题

当我使用

System.Web.Mvc.dll版本5.2.2.0 – >它会导致错误内容typestext / html; charset = utf-8的响应消息

但是当我使用System.Web.Mvc.dll版本4.0.0.0或更低 – >它工作正常

我不知道不同版本的DLL问题的原因,但通过改变DLL的verison它为我工作。

当您在WCF项目中添加其他Project的引用时,甚至会生成此错误,并且此引用项目具有不同版本的System.Web.Mvc DLL,或者可能是任何其他DLL。

X ++ binding = endPoint.get_Binding(); binding.set_UseDefaultWebProxy(false); binding = endPoint.get_Binding(); binding.set_UseDefaultWebProxy(false);

我通过在web.config中设置UseCookies来解决这个问题。

  <system.web> <sessionState cookieless="UseCookies" /> 

并设置enableVersionHeader

  <system.web> <httpRuntime targetFramework="4.5.1" enableVersionHeader="false" executionTimeout="1200" shutdownTimeout="1200" maxRequestLength="103424" /> 

我有一个类似的问题。 我通过改变解决了它

 <basicHttpBinding> 

 <basicHttpsBinding> 

还改变了我的URL使用https://而不是http://。

同样在<endpoint>节点中,更改

 binding="basicHttpBinding" 

 binding="basicHttpsBinding" 

这工作。

如果你同时使用https请求和wshttpbinding,那么我通过使用下面的configuration更改来解决它。

  <security mode="TransportWithMessageCredential"> <transport clientCredentialType="None" /> <message clientCredentialType="Certificate" /> </security> 

对于我来说,当我在Web.config中注释以下行时,问题就解决了

 <httpErrors errorMode="Detailed" />