【发布时间】:2010-12-18 21:24:18
【问题描述】:
我真的很难过。我已经尝试了所有可以想象的工具,检查了所有可以使用的资源,但仍然无法理解这一点。
我有一个 WCF 服务,旨在处理专有 XML 格式的普通旧 XML 请求(这是旧的 ASMX 转换)。我已经使用 WCF 3.5 设置了服务以接受普通的 HTTP POST 请求,只要请求标头中的 Content-Type 未设置为“text/xml”(或任何其他 XML 或 JSON 类型,例如“文本/json”或“应用程序/json”)。
换句话说,如果我将请求的内容类型设置为“text”或“text/plain”甚至“whatever”,则服务会按预期工作。但是,当我将内容类型设置为“text/xml”时,服务会因 HTTP 400 错误请求而失败。
而且我未能找到一种方法来调试失败以获取更多信息。实际的服务方法永远不会被调用,并且实现 IErrorHandler 也没有捕获错误。我怀疑我的 WCF 配置或 IIS 7 设置有问题,但我完全不知道发生了什么。
这是我的服务合同:
using System.ServiceModel;
using System.ServiceModel.Web;
using System.IO;
namespace App.api.v_1_1
{
[ServiceContract]
public interface IPlainXmlWebServiceViaHttpPost
{
[WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
Stream Process(Stream xml);
}
}
这是我的服务实现:
using System.IO;
using System.ServiceModel.Activation;
using App.api.v_1_0;
using System.ServiceModel;
namespace App.api.v_1_1
{
[ServiceBehavior(Namespace = Constants.WebServiceNamespace)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class PlainXmlWebServiceViaHttpPost : IPlainXmlWebServiceViaHttpPost
{
public Stream Process(Stream request)
{
return request.ProcessTextStreamWith(PoxProcessor.Process);
}
}
}
基本上,它只是将传入的 Stream 转换为字符串,对该字符串执行某些操作,然后返回转换回 Stream 的字符串响应。这个想法是能够将任意 XML 传递给服务并返回任意 XML。
最后,这是 web.config 的相关部分:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<bindings>
<webHttpBinding>
<binding name="poxBinding" maxReceivedMessageSize="655360">
<readerQuotas maxArrayLength="655360" />
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="poxBehavior">
<serviceDebug httpHelpPageEnabled="false" includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="poxBehavior" name="App.api.v_1_1.PlainXmlWebServiceViaHttpPost">
<endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding"
bindingConfiguration="poxBinding" contract="App.api.v_1_1.IPlainXmlWebServiceViaHttpPost" />
</service>
</services>
</system.serviceModel>
我一直在尝试各种解决方法来让 WCF/IIS 与我合作,但都失败了。有谁知道什么会导致请求预期的内容类型返回 HTTP 400,但所有其他内容类型都按预期处理?
【问题讨论】:
-
我认为一旦你设置了格式,内置的序列化就会启动,它可能不知道如何处理流。想要Stream的话,能不能切换到basicHttpBinding,开启streaming?