【发布时间】:2026-01-28 08:20:02
【问题描述】:
我正在测试我的服务的故障和异常处理,并看到奇怪的结果。
我有一个配置了两个端点的服务合同。一个是basicHttpBinding,另一个是wsHttpBinding。在我的机器上本地我有一个测试应用程序客户端和服务器正在运行。我可以成功调用这两个 Web 服务绑定并获得我正在寻找的异常,这正是我正在寻找的。但是当我将代码推送到开发服务器时,我只能成功调用 wsHttpBinding 端点。基本端点返回一条奇怪的消息。例外是“远程服务器返回错误:(500) 内部服务器错误。”
异常消息是:
"响应消息的内容类型 text/html 与绑定的内容类型不匹配 (text/xml; charset=utf-8)。如果使用自定义编码器,请确保 IsContentTypeSupported 方法正确实现。响应的前1024字节是:在此处插入一堆废话html”
这是配置错误吗? IIS 错误?防火墙问题?我已经在服务器上运行了 WCF 日志记录和跟踪。我的代码被击中得很好,它抛出了一个错误异常。 basicHttpBinding 上的消息头是空的,但我不确定这是否有问题。我也检查了 IIS 日志。它显示了 500 个错误,仅此而已。还有什么需要检查的吗?
我的端点:
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IEOIEligible_NONSSL"
name="IEOIEligible_Svc_endpoint_NONSSL"
contract="Engine.IEOIEligible">
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
name="Mex_Svc_endpoint"
contract="IMetadataExchange" />
<endpoint
address="/basic"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IEOIEligible"
name="basicEOIEndpoint"
contract="Engine.IEOIEligible" />
[编辑]
绑定配置:
基本的HttpBinding
<binding name="BasicHttpBinding_IEOIEligible" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="Certificate" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Certificate" algorithmSuite="Default" />
</security>
</binding>
wsHttpBinding
<binding name="WSHttpBinding_IEOIEligible_NONSSL" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferPoolSize="524288" maxReceivedMessageSize="524288" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="819200" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
</binding>
来自 WCF 跟踪文件:
调用成功(wsHttp):
<ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/MessageTraceRecord">
<MessageProperties>
<Encoder>application/soap+xml; charset=utf-8</Encoder>
<AllowOutputBatching>False</AllowOutputBatching>
</MessageProperties>
<MessageHeaders>
<Action d4p1:mustUnderstand="1" xmlns:d4p1="http://www.w3.org/2003/05/soap-envelope" xmlns="http://www.w3.org/2005/08/addressing">http://tempuri.org/IEOIEligible/VerifyEOIRequiredEOIEligibilityFaultFault</Action>
<RelatesTo xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:5863c107-b721-4b4c-88b8-0d03c22175d1</RelatesTo>
<ActivityId CorrelationId="a498440c-2e5a-4298-9bc5-ade08d51933c" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">00000000-0000-0000-0000-000000000000</ActivityId>
</MessageHeaders>
</ExtendedData>
服务器 500 (basicHttp)
<ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/MessageTraceRecord">
<MessageProperties>
<Encoder>text/xml; charset=utf-8</Encoder>
<AllowOutputBatching>False</AllowOutputBatching>
</MessageProperties>
<MessageHeaders></MessageHeaders>
</ExtendedData>
[编辑]
我能够通过 Microsoft WCF 测试客户端使用 basicHttpBinding 从服务中获得响应,但这只是当我没有在服务器上抛出 FaultException 时,这意味着服务返回了愉快的路径。这部分我不关心,谢天谢地。
我正在对客户端异常处理进行单元测试,并且服务器在基本而不是 wsHttpBinding 上返回 500。 wsHttpBinding 返回嵌入在肥皂体内的正确 SOAP 错误,但基本不是。 Soap 错误在本地运行时返回 basicHttpBinding,而不是从真正的服务器运行。
【问题讨论】:
-
能否为问题添加绑定配置?
-
@dharnitski 我添加了绑定配置。谢谢!
-
你能用 Fiddler 之类的东西来查看调用 BasicHttpBinding 时返回的 HTML 页面是什么吗?此外,不要使用消息日志,而是尝试使用 WCF tracing(诊断),它应该包含有关错误的更多信息。