WCF服务返回HTTP 400,但仅当Content-Type设置为"text/xml"或"text/json"时

ada*_*per 4 c# iis wcf

我真的很难过.我已经尝试了所有可以想象的工具,检查了我能够的每一种资源,但仍然无法理解这一点.

我有一个WCF服务,旨在以专有的XML格式处理普通的旧XML请求(这是一个旧的ASMX转换).我使用WCF 3.5设置服务以接受普通的HTTP POST请求,只要请求标头中的Content-Type未设置为"text/xml"(或任何其他XML或JSON类型,它就可以很好地工作)例如"text/json"或"application/json").

换句话说,如果我将请求的内容类型设置为"text"或"text/plain"或甚至"what",则服务按预期工作.但是,当我将内容类型设置为"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);
 }
}
Run Code Online (Sandbox Code Playgroud)

这是我的服务实现:

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);
        }
 }
}
Run Code Online (Sandbox Code Playgroud)

基本上它只是将传入的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>
Run Code Online (Sandbox Code Playgroud)

我一直在尝试各种解决方法让WCF/IIS与我合作,但都失败了.有谁知道什么会导致请求预期的内容类型返回HTTP 400,但所有其他内容类型都按预期处理?

Dar*_*rov 7

您是否在文件中使用了WebServiceHostFactory.svc:

<%@ ServiceHost 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory"   
    Service="App.api.v_1_1.PlainXmlWebServiceViaHttpPost" 
%>
Run Code Online (Sandbox Code Playgroud)