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

C# WCF服务客户端:内容类型text/html;响应消息的charset=utf-8与绑定的内容类型不匹配,c#,.net,wcf,web-services,C#,.net,Wcf,Web Services,我在本地IIS服务器上运行了一个WCF服务。我将其作为服务引用添加到一个C#网站项目中,它添加了fine并自动生成代理类 但是,当我尝试调用任何服务合同时,会出现以下错误: 说明:测试期间发生未处理的异常 当前web请求的执行。 请查看堆栈跟踪以了解更多信息 有关错误和位置的信息 它起源于代码 异常详细信息:System.ServiceModel.ProtocolException: 内容类型text/html; 字符集=响应消息的utf-8 与的内容类型不匹配 绑定(应用程序/soap+xml

我在本地IIS服务器上运行了一个WCF服务。我将其作为服务引用添加到一个C#网站项目中,它添加了fine并自动生成代理类

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

说明:测试期间发生未处理的异常 当前web请求的执行。 请查看堆栈跟踪以了解更多信息 有关错误和位置的信息 它起源于代码

异常详细信息:System.ServiceModel.ProtocolException: 内容类型text/html; 字符集=响应消息的utf-8 与的内容类型不匹配 绑定(应用程序/soap+xml; 字符集=utf-8)。如果使用自定义 编码器,确保 IsContentTypeSupported方法是 执行得当。前1024 响应的字节数为:' 功能 bredir(d,u,r,v,c){var w,h,wd,hd,bi;var b=假;var p=假;var s=[300250,假],[250250,假],[240400,假],[336280,假],[180150,假],[468,60,假],[234,60,假],[88,31,假],[120,90,假],[120240,假],[125125,假],[728,90,假],[160600,假],[300125,假],[530300,假],[190200,假],[470250,假],[720300,真],[500350,真],[550480,true]];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服务进行通信,控制台应用程序能够很好地调用方法,而不会出现此错误

下面是我的配置文件摘录

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>

这是我第一次尝试创建WCF,所以它非常新。非常感谢您的帮助。

来自web服务器的HTML响应通常表示提供了错误页面,而不是来自WCF服务的响应。我的第一个建议是检查您运行WCF客户端的用户是否有权访问re源。

使用客户端正常运行时使用的相同Windows凭据,尝试在承载服务的服务器上的web浏览器中浏览

我想象IIS显示了一条某种描述的html错误消息,而不是像预期的那样返回xml

当您有一个执行internet筛选的http代理服务器时,也会发生这种情况。我对ContentKeeper的经验是,它拦截任何http/https流量,并将其作为“非托管内容”进行阻止-我们得到的只是一条html错误消息。为避免此情况,您可以将代理服务器异常规则添加到Internet Explorer,以便代理服务器不会拦截到您站点的流量:

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


在我的例子中,URL重写规则弄乱了我的服务名称,它被重写为小写,我得到了这个错误


请确保不要使用小写的WCF服务调用。

与许多服务调用一样,在我的情况下,由于一个错误,我也收到了此消息。遗憾的是,我只能读取html错误页面的CSS


我的问题的根源也是服务器上的重写规则。它正在将http重写为https。

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


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

发生的情况是,您正在尝试使用wsHttpBind访问该服务,默认情况下,wsHttpBind使用安全加密消息(安全消息)。 另一方面,netTcpBind使用安全的加密通道(安全传输)……但basicHttpBind根本不需要任何安全性,可以匿名访问

因此,在服务器端,将其添加\更改到您的配置中

<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" > 


应该可以了。

我尝试了上面的所有建议,但最终成功的是将应用程序池管理管道从集成模式更改为经典模式。
它在自己的应用程序池中运行-但它是第一个.NET 4.0服务-所有其他服务都在.NET 2.0上使用集成管道模式。 它只是一个标准的WCF服务,使用的是https,但在Server2008(不是R2)上使用的是IIS7(不是7.5)。

X++
binding=endPoint.get_binding();

binding.set_UseDefaultWebProxy(false);

即使不使用网络代理,在代理对话框中打开“自动检测设置”也会关闭此异常


我也遇到过类似的情况,但客户端配置使用的是basicHttpBinding。问题是该服务使用的是SOAP 1.2,您不能在basicHttpBinding中指定SOAP 1.2。我修改了客户端配置,改为使用customBinding,一切正常。以下是我的customBinding的详细信息,以供参考我试图使用的服务是通过HTTPS使用UserNameOverTransport

<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>

我通过在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>


这是有效的。

在我的WCF系列项目中,这个问题是由于引用了System.Web.Mvc.dll的不同版本。因此,它可能是dll的不同版本的兼容性问题

当我使用

System.Web.Mvc.dll版本5.2.2.0->it thor
<basicHttpBinding>
<basicHttpsBinding>
binding="basicHttpBinding" 
binding="basicHttpsBinding"
 <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="None" />
                    <message clientCredentialType="Certificate" />
                </security>
<httpErrors errorMode="Detailed" />