使用 basicHttpBinding 的 WCF 服务仍然发送内容类型“text/xml”

发布于 2024-12-14 13:10:26 字数 2617 浏览 1 评论 0原文

我正在编写一个在 IIS 上运行的 WCF 服务,并且我有一个客户,其客户端只能使用 SOAP 1.1。

除此之外,他们需要内容类型为“application/soap+xml;charset=utf-8”。我的 WCF 服务正在发送“text/xml; charset=utf-8”。

尝试写客户端的客户向我转发了一条错误消息:

HTTP 415:无法处理消息,因为内容类型 '文本/xml; charset=utf-8' 不是预期的类型 '应用程序/soap+xml;字符集=utf-8

浏览网络,我发现了一些这样的博客页面: WCF 错误 - 请求失败,HTTP 状态为 415

这让我认为从 wsHttpBinding 切换到 basicHttpBinding 可以解决这个问题。因此,我更改了 web.config 中的绑定,并修改了我自己的测试客户端以显式创建带有 BasicHttpBinding 的端点。在我自己的测试中,一切都运行良好(在 Visual Studio 的 Dev Server 中运行该服务,在我自己的计算机上的 IIS7 中运行它,并在我们的一台测试服务器上的 IIS6 中运行它。)

但在我给客户之前请注意,并要求他们看看我的更改是否对他们有用,我启动了 Fiddler 来窃听实际流量。

根据 Fiddler 的说法,我仍在发送“text/xml; charset=utf-8”。

那么,我该如何解决这个问题呢?

<system.serviceModel>
    <services>
        <service behaviorConfiguration="myBehavior" name="myName">
            <endpoint 
                    address=""
                    binding="basicHttpBinding" 
                    behaviorConfiguration="flatWsdlFileEndpointBehavior" 
                    bindingNamespace="http://myNamespace"
                    contract="myContract">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="myBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="flatWsdlFileEndpointBehavior">
                <wsdlExtensions location="myUrl" singleFile="true" />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <basicHttpBinding>
        </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <extensions>
        <behaviorExtensions>
            <add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </behaviorExtensions>
    </extensions>
</system.serviceModel>

I am writing a WCF service, running on IIS, and I have a customer whose client can only talk SOAP 1.1.

Among other things, they need the content type to be "application/soap+xml; charset=utf-8". My WCF service is sending "text/xml; charset=utf-8".

The customer who's trying to write the client forwarded me an error message:

HTTP 415: Cannot process the message because the content type
'text/xml; charset=utf-8' was not the expected type
'application/soap+xml; charset=utf-8

Browsing around the net, I found a number of blog pages like this: WCF Error - The request failed with HTTP status 415.

Which made me think that switching from wsHttpBinding to basicHttpBinding would fix this. So I changed the binding in the web.config, and modified my own test client to explicitly create an endpoint with a BasicHttpBinding. And that all worked fine, in my own tests (both running the service in Visual Studio's Dev Server, and running it in IIS7 on my own machine, and running it in IIS6 on one of our test servers.)

But before I gave the customer a head's up, and asked them to see if my changes would work for them, I fired up Fiddler to eavesdrop on the actual traffic.

And according to Fiddler, I'm still sending "text/xml; charset=utf-8".

So, how do I fix that?

<system.serviceModel>
    <services>
        <service behaviorConfiguration="myBehavior" name="myName">
            <endpoint 
                    address=""
                    binding="basicHttpBinding" 
                    behaviorConfiguration="flatWsdlFileEndpointBehavior" 
                    bindingNamespace="http://myNamespace"
                    contract="myContract">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="myBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="flatWsdlFileEndpointBehavior">
                <wsdlExtensions location="myUrl" singleFile="true" />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <basicHttpBinding>
        </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <extensions>
        <behaviorExtensions>
            <add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
        </behaviorExtensions>
    </extensions>
</system.serviceModel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残疾 2024-12-21 13:10:26

那么他们需要 SOAP 1.1 还是 application/soap+xml?字符集=utf-8?因为 SOAP 1.1 规范规定请求必须使用 text/xml 作为媒体类型。 application/soap+xml 是 SOAP 1.2 的媒体类型。强制 WCF 将 SOAP 1.1 与 application/soap+xml(= 无效 SOAP)一起使用将需要比更改绑定更大的更改。您将需要一些自定义消息编码器或传输通道。

So do they need SOAP 1.1 or application/soap+xml; charset=utf-8? Because SOAP 1.1 specification says that the request must have text/xml as the media type. application/soap+xml is media type for SOAP 1.2. Forcing WCF to use SOAP 1.1 with application/soap+xml (= invalid SOAP) would require bigger changes than changing the binding. You will need some custom message encoder or perhaps transport channel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文