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

时间:2011-03-09 09:47:42

标签: c# .net wcf web-services

我在本地IIS服务器上运行了WCF服务。我已将其添加为C#Website Project的服务引用,并且可以自动添加并自动生成代理类。

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

  

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

     

异常详细信息: System.ServiceModel.ProtocolException:   内容类型text / html;   响应消息的charset = utf-8   与内容类型不匹配   绑定(application / soap + xml;   字符集= UTF-8)。如果使用自定义   编码器,请确保   IsContentTypeSupported方法是   实施得当。第一个1024   响应的字节是:'        功能   bredir(d,u,r,v,c){var w,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,真]];如果(typeof运算(window.innerHeight)== '编号'){H = window.innerHeight; W = window.innerWidth;}否则   如果(typeof运算(document.body.offsetHeight)== '编号'){H = document.body.offsetHeight; W = document.body.offsetWidth;}对于(VAR   I = 0;我

我还有一个控制台应用程序,它也与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>

网站项目服务客户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>

这是我第一次尝试创建一个WCF,所以这一切都很新。非常感谢任何帮助。

21 个答案:

答案 0 :(得分:25)

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

我认为IIS正在显示某些描述的html错误消息,而不是按预期返回xml。

当您拥有执行Internet过滤的http代理服务器时,也会发生这种情况。我对ContentKeeper的体验是拦截任何http / https流量并将其阻止为“非托管内容” - 我们得到的只是一条HTML错误消息。为避免这种情况,您可以将代理服务器例外规则添加到Internet Explorer,以便代理不会拦截您网站的流量:

控制面板&gt;互联网选项&gt;连接&gt; LAN设置&gt;高级&gt;代理设置

enter image description here

答案 1 :(得分:15)

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

答案 2 :(得分:11)

我有类似的问题。我通过改变

解决了这个问题
<basicHttpBinding>

<basicHttpsBinding>

并将我的网址更改为使用https://而不是http://。

同样在&lt; endpoint&gt;节点,改变

binding="basicHttpBinding" 

binding="basicHttpsBinding"

这很有用。

答案 3 :(得分:5)

正在发生的是您尝试使用wsHttpBind访问该服务,wsHttpBind默认使用安全加密邮件(安全邮件)。 另一方面,netTcpBind使用安全加密通道。 (安全传输)...但是BasicHttpBind,根本不需要任何安全性,并且可以访问匿名

SO。在服务器端,添加\将其更改为您的配置。

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

应该这样做。

答案 4 :(得分:1)

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

确保您没有小写WCF服务电话。

答案 5 :(得分:1)

与许多人一样,在我的情况下,由于错误,我也得到了这个。遗憾的是,我只能阅读html错误页面的CSS。

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

答案 6 :(得分:1)

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

您可能还想检查用于调用服务的凭据。

答案 7 :(得分:1)

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

答案 8 :(得分:1)

即使您没有使用网络代理,也可以自动检测设置&#39;在代理对话框中,这个例外就会消失。

enter image description here

答案 9 :(得分:1)

我有类似的情况,但客户端配置使用basicHttpBinding。问题是该服务使用的是SOAP 1.2,您无法在basicHttpBinding中指定SOAP 1.2。我修改了客户端配置以使用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>

答案 10 :(得分:1)

在我的WCF服务项目中,此问题是由System.Web.Mvc.dll的不同版本引用引起的。所以它可能是DLL的不同版本的兼容性问题

当我使用

System.Web.Mvc.dll版本5.2.2.0 - &gt;它引出了错误内容类型text / html;响应消息的charset = utf-8

但是当我使用时 System.Web.Mvc.dll版本4.0.0.0或更低版本 - &gt;它工作正常

我不知道不同版本DLL问题的原因,但通过更改DLL的版本,它对我有用。

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

答案 11 :(得分:0)

我在我的 asp.net core 5 web api 项目中遇到了类似的问题来调用soap服务。我通过以下方式解决了:

1.更改其 url : return new System.ServiceModel.EndpointAddress("http://ourservice.com/webservices/service1.asmx?wsdl");到 https://ourservice.com/...

  1. 在它的 Reference.cs 中使用这些配置:

私有静态 System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration) { if ((endpointConfiguration == EndpointConfiguration.TaxReturnSoap)) { System.ServiceModel.BasicHttpsBinding 结果 = new System.ServiceModel.BasicHttpsBinding(); result.TextEncoding = System.Text.Encoding.UTF8; 结果.MaxBufferSize = int.MaxValue; result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max; result.MaxReceivedMessageSize = int.MaxValue; result.AllowCookies = true; 返回结果; } if ((endpointConfiguration == EndpointConfiguration.TaxReturnSoap12)) { System.ServiceModel.Channels.CustomBinding 结果 = new System.ServiceModel.Channels.CustomBinding(); System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement(); textBindingElement.WriteEncoding = System.Text.Encoding.UTF8; textBindingElement.MessageVersion = System.ServiceModel.Channels.MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap12, System.ServiceModel.Channels.AddressingVersion.None); result.Elements.Add(textBindingElement); System.ServiceModel.Channels.HttpsTransportBindingElement httpBindingElement = new System.ServiceModel.Channels.HttpsTransportBindingElement(); httpBindingElement.AllowCookies = true; httpBindingElement.MaxBufferSize = int.MaxValue; httpBindingElement.MaxReceivedMessageSize = int.MaxValue; result.Elements.Add(httpBindingElement); 返回结果; } throw new System.InvalidOperationException(string.Format("Could not find endpoint with name '{0}'.", endpointConfiguration)); }

答案 12 :(得分:0)

就我而言,这是因为我的 WCF web.config 将返回字符作为第一行。更令人沮丧的是,它位于负载平衡器后面,并且只发生了一半的时间(两个 web.config 中只有一个出现此问题)。

答案 13 :(得分:0)

我的解决方案非常简单:从应用程序备份所有内容,将其卸载,从其余文件夹(而不是文件夹)删除所有内容,这样我就不必再次授予相同的权限了,然后从备份中复制回文件

答案 14 :(得分:0)

注意:如果目标服务器端点正在使用安全套接字层(SSL)证书

将.config设置从With DUPGASJOB as ( select [dbo].[RM-JOB].[Job-No] ,loc.[Place-Ref] ,Address ,Address##2 ,place.[Post-Code] ,loc.[Appointment-Area-Code] ,aa.Description ,comp.[Make-Code] ,comp.[Model-Code] ,CONVERT(VARCHAR,cycles.[START-DATE],3) CompletedDate --,CONVERT(VARCHAR,comp.[INSTALLED-DATE],3) InstalledDate ,CONVERT(VARCHAR,instdate.[maxInsDate],3) RecentInstalledDate ,isnull([OC-EMPLOYEE].INITIALS +' '+[OC-EMPLOYEE].SURNAME, 'No Officer') as OperativeName ,ROW_NUMBER() OVER(partition by loc.[PLACE-REF] ORDER BY [MAKE-CODE] desc, instdate.[maxInsDate] asc) AS [Row_Number] ,datediff(DAY, instdate.[maxInsDate], getdate()) NoOfDays ,case when datediff(DAY, instdate.[maxInsDate], getdate()) < 8 then 'Under7Days' else 'Over8Days' end as NoDayCompleted from [ih-location] loc join [co-place] place on place.[PLACE-REF] = loc.[PLACE-REF] left join [IH_APPOINTMENT-AREA] aa on aa.[APPOINTMENT-AREA-CODE] = loc.[APPOINTMENT-AREA-CODE] join [dbo].[RM-LOC-COMPONENT] comp on comp.[PLACE-REF] = loc.[PLACE-REF] and comp.COMPONENT in ('GASBOIL','GASLPG') AND (comp.[END-DATE] is null or comp.[END-DATE] > GETDATE()) and comp.component in ('GASBOIL','GASLPG') LEFT JOIN [dbo].[RM-LOC-COMPONENT-CYCLES] cycles ON cycles.[PLACE-REF] = comp.[PLACE-REF] and cycles.[LIFE-CYCLE] in ('GSOR','GSER') and cycles.[BAR-CODE] = comp.[BAR-CODE] AND comp.component = cycles.component AND comp.[SUB-LOC-CODE] = cycles.[SUB-LOC-CODE] inner join [dbo].[RM-JOB] on [RM-JOB].[PLACE-REF] = place.[place-ref] left outer join [dbo].[OC-EMPLOYEE] on [OC-EMPLOYEE].[CLOCK-NUMBER] = [RM-JOB].[CONTRACTOR-OPERATIVE] inner join (select [PLACE-REF], max([INSTALLED-DATE]) maxInsDate from [dbo].[RM-LOC-COMPONENT] where COMPONENT in ('GASBOIL','GASLPG') group by [PLACE-REF]) instdate on instdate.[PLACE-REF] = place.[PLACE-REF] where [dbo].[RM-JOB].[SOURCE] = '09.SERV' and [SERVICING-JOB] = 1 and [JOB-TYPE] = 'GSER' and [dbo].[RM-JOB].[JOB-STATUS-MAP] in ('FINC', 'PRAC') ) select * from DUPGASJOB where DUPGASJOB.[Row_Number] > 0 -- and [PLACE-REF] = '4383'--'5703'-- and NoDayCompleted in ('Under7Days') and DUPGASJOB.[Job-No] = '10201102' order by [PLACE-REF] 更改为basicHttpBinding

我确定,它将解决您的问题。

答案 15 :(得分:0)

对我来说,它是指向错误数据库服务器的Web应用程序连接字符串。

答案 16 :(得分:0)

嗨, 在我的情况下,出现此错误是因为Web服务的应用程序池具有错误的32/64位设置。因此,此错误需要以下修复:您转到IIS,选择Web服务的站点,转到“高级”设置并获取应用程序池。然后转到“应用程序池”,选择它,转到“高级设置...”,选择“启用32位应用程序”,然后根据您的Web服务的32/64位类型将其设为“启用”或“禁用”。 如果设置为True,则意味着它仅允许32位应用程序,因此对于64位应用程序,必须将其设置为“禁用”(默认)。

答案 17 :(得分:0)

对我来说,当我在Web.config

中评论以下行时问题得到了解决
<httpErrors errorMode="Detailed" />

答案 18 :(得分:0)

如果您同时使用wshttpbinding和https请求,那么我使用以下配置更改解决了这个问题。

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

答案 19 :(得分:0)

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

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

并设置enableVersionHeader

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

答案 20 :(得分:0)

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