gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:28:52 28 4
gpt4 key购买 nike

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

我有一个 WCF 服务,旨在处理专有 XML 格式的普通旧 XML 请求(这是旧的 ASMX 转换)。我已经使用 WCF 3.5 设置服务来接受纯 HTTP POST 请求,只要请求 header 中的 Content-Type 未设置为“text/xml”(或任何其他 XML 或 JSON 类型,例如“文本/json”或“应用程序/json”)。

换句话说,如果我将请求的内容类型设置为“text”或“text/plain”甚至“whatever”,该服务将按预期工作。但是,当我将内容类型设置为“text/xml”时,服务失败并显示 HTTP 400 错误请求。

而且我未能找到一种方法来调试失败以获取更多信息。实际的服务方法从未被调用,实现 IErrorHandler 也没有捕获到错误。我怀疑我的 WCF 配置或 IIS 7 设置有问题,但我完全不知道发生了什么。

这是我的服务契约(Contract):

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,但所有其他内容类型都按预期处理?

最佳答案

你用过WebServiceHostFactory吗?在您的 .svc 文件中:

<%@ ServiceHost 
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
Service="App.api.v_1_1.PlainXmlWebServiceViaHttpPost"
%>

关于c# - WCF 服务返回 HTTP 400,但仅当 Content-Type 设置为 "text/xml"或 "text/json"时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1773147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com