You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WCF服务调用ContentType不匹配错误排查请求

WCF调用ContentType不匹配错误排查与解决

错误信息

System.ServiceModel.ProtocolException
HResult=0x80131501
Message=The content type is text/html; charset=utf-8 of the response message does not match the content type of the association (text/xml; charset=utf-8). If you're using a custom encoder, make sure the IsContentTypeSupported service action is properly implemented. The first 1024 bytes of the response were:...

核心原因

该错误本质是客户端期望接收text/xml格式的SOAP响应,但实际收到了text/html内容——通常是IIS返回的错误页面(如404、401、500页面),而非正常的WCF服务响应。结合你提供的配置,可能的具体原因及解决方法如下:

1. 服务端身份验证配置冲突

服务端basicHttpBinding的security节点同时配置了transport clientCredentialType="Windows"和message clientCredentialType="UserName",这在TransportCredentialOnly模式下是冲突的,会导致身份验证逻辑异常,返回HTML错误页面。

解决方法:
修改服务端web.config的basicHttpBinding security配置,移除message节点:

<security mode="TransportCredentialOnly">
  <transport clientCredentialType="Windows" realm="" />
  <!-- 移除以下冲突配置 -->
  <!-- <message clientCredentialType="UserName" /> -->
</security>

确保客户端与服务端的security模式、clientCredentialType完全一致(客户端当前配置是正确的)。

2. 服务端地址或可用性问题

客户端请求的服务地址不正确,或服务在IIS中未正常启动,导致请求落到IIS默认页面或错误页面。

解决方法:

  • 直接在浏览器访问服务端点地址(如http://你的服务域名/Service1.svc),确认能正常显示WCF服务说明页面,而非404/500错误。
  • 检查IIS应用程序池状态:确保池已启动,且.NET版本与服务目标框架匹配。
  • 查看IIS日志(路径通常为C:\inetpub\logs\LogFiles),定位请求对应的状态码,确认是权限错误(401)、地址错误(404)还是服务器内部错误(500)。

3. 绑定配置不兼容

客户端与服务端的绑定核心参数存在差异,可能导致请求处理异常。

解决方法:

  • 对齐maxReceivedMessageSize、readerQuotas等核心参数:客户端当前maxReceivedMessageSize为2147483646,服务端为2147483647,建议统一为相同值。
  • 确认客户端端点配置中指定的bindingConfiguration名称与服务端的绑定名称对应(客户端用BasicHttpBinding_IService1,服务端用basicHttpBinding,只要客户端端点正确关联即可)。

4. 启用WCF日志排查细节

若以上方法未解决问题,可启用服务端日志捕获详细错误:
在服务端web.config中添加诊断配置:

<system.diagnostics>
  <sources>
    <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
      <listeners>
        <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\logs\WcfTrace.svclog" />
      </listeners>
    </source>
  </sources>
</system.diagnostics>

生成日志后使用WCF Service Trace Viewer工具分析具体错误栈。

内容的提问来源于stack exchange,提问作者h.z.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:00:15