简体   繁体   English

包含无法识别的http正文格式值“ Xml”

[英]Contains an unrecognized http body format value 'Xml'

There are a number of similar questions on stack overflow, but none have helped me formulate a solution. 堆栈溢出有很多类似的问题,但是没有一个问题可以帮助我制定解决方案。

Question below is the closest, but I couldn't get it to work and my other services stopped working when I tried to implement it. 下面的问题是最接近的问题,但是当我尝试实现它时,我无法使其正常工作,并且其他服务也停止了工作。

WCF input huge XML as Stream with Content-Type: xml/text WCF输入巨大的XML作为Content-Type的流:xml / text

Error: Incoming message for operation 'IncomingXML' (contract ... with namespace ...) contains an unrecognized http body format value 'Xml'. 错误:操作'IncomingXML'的传入消息(带有名称空间的合同...)包含无法识别的http正文格式值'Xml'。 The expected body format value is 'Raw'. 预期的正文格式值为“ Raw”。 This can be because a WebContentTypeMapper has not been configured on the binding. 这可能是因为尚未在绑定上配置WebContentTypeMapper。 See the documentation of WebContentTypeMapper for more details. 有关更多详细信息,请参见WebContentTypeMapper的文档。

Basically I need this and only this web service to accept "application/xml". 基本上,我只需要这个Web服务来接受“ application / xml”。 At the moment it accepts only "raw", the service fails when the call has "application/xml" set. 目前仅接受“原始”,调用设置为“ application / xml”后,服务将失败。

The problem is two parts, one allow the service to accept xml and two, don't effect the other web services. 问题有两部分,一是允许服务接受xml,二是不影响其他Web服务。 This is the only service that accepts incoming xml. 这是唯一接受传入xml的服务。

Does anyone have any suggestions? 有没有人有什么建议? If I can't get this to work without disrupting the other web services. 如果我不能在不中断其他Web服务的情况下使它正常工作。

Contract 合同

[OperationContract]
[WebInvoke(Method = "POST")]
void IncomingXML(Stream xml);

Service 服务

public void IncomingXML(Stream xml)
{
}

Web.config: Web.config文件:

<system.serviceModel>
    <extensions>
        ...
    </extensions>
    <diagnostics>
        ...
    </diagnostics>
    <behaviors>
        <endpointBehaviors>
            <behavior name="jsonEndpointBehavior">
                <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" defaultBodyStyle="WrappedRequest" faultExceptionEnabled="true" />
                <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                <endpointDiscovery enabled="true" />
            </behavior>
            <behavior name="xmlEndpointBehavior">
                <webHttp helpEnabled="false" defaultBodyStyle="WrappedRequest" defaultOutgoingResponseFormat="Xml" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
                <dataContractSerializer maxItemsInObjectGraph="2147483647" />
            </behavior>
            <behavior name="rawEndpointBehavior">
                <webHttp helpEnabled="false" defaultBodyStyle="WrappedRequest" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
                <dataContractSerializer maxItemsInObjectGraph="2147483647" />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="serviceBehavior">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <bindings>
        <webHttpBinding>
            <binding name="jsonBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" crossDomainScriptAccessEnabled="true" />
            <binding name="jsonsBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" crossDomainScriptAccessEnabled="true">
                <security mode="Transport">
                    <transport clientCredentialType="None" />
                </security>
            </binding>
            <binding name="xmlBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" crossDomainScriptAccessEnabled="true" />
            <binding name="xmlsBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" crossDomainScriptAccessEnabled="true">
                <security mode="Transport">
                    <transport clientCredentialType="None" />
                </security>
            </binding>
            <binding name="rawBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" crossDomainScriptAccessEnabled="true" />
            <binding name="rawsBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" crossDomainScriptAccessEnabled="true">
                <security mode="Transport">
                    <transport clientCredentialType="None" />
                </security>
            </binding>
        </webHttpBinding>
        <basicHttpBinding>
            <binding name="soapBinding" />
        </basicHttpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="serviceBehavior" name="Namespace.Web.WCF.Service">
            <endpoint address="json" behaviorConfiguration="jsonEndpointBehavior" binding="webHttpBinding" bindingConfiguration="jsonBinding" name="jsonHttpEndpoint" bindingName="Namespace.Service.JsonHttp" contract="Namespace.Web.WCF.IService" />
            <endpoint address="json" behaviorConfiguration="jsonEndpointBehavior" binding="webHttpBinding" bindingConfiguration="jsonsBinding" name="jsonHttpsEndpoint" bindingName="Namespace.Service.JsonHttps" contract="Namespace.Web.WCF.IService" />
            <endpoint address="xml" behaviorConfiguration="xmlEndpointBehavior" binding="webHttpBinding" bindingConfiguration="xmlBinding" name="xmlHttpEndpoint" bindingName="Namespace.Service.XmlHttp" contract="Namespace.Web.WCF.IService" />
            <endpoint address="xml" behaviorConfiguration="xmlEndpointBehavior" binding="webHttpBinding" bindingConfiguration="xmlsBinding" name="xmlHttpsEndpoint" bindingName="Namespace.Service.XmlHttps" contract="Namespace.Web.WCF.IService" />
            <endpoint address="" behaviorConfiguration="xmlEndpointBehavior" binding="webHttpBinding" bindingConfiguration="rawBinding" name="rawHttpEndpoint" bindingName="Namespace.Service.RawHttp" contract="Namespace.Web.WCF.IService" />
            <endpoint address="" behaviorConfiguration="xmlEndpointBehavior" binding="webHttpBinding" bindingConfiguration="rawsBinding" name="rawHttpsEndpoint" bindingName="Namespace.Service.RawHttps" contract="Namespace.Web.WCF.IService" />

            <endpoint address="web" binding="basicHttpBinding" bindingName="Namespace.Service" contract="Namespace.Web.WCF.IService" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
        </service>

This blog post has the solution: http://kimcuhoang.blogspot.be/2011/04/solving-raw-issue-with-wcf-and-content_13.html 该博客文章提供了解决方案: http : //kimcuhoang.blogspot.be/2011/04/solving-raw-issue-with-wcf-and-content_13.html

Basically you have to override the default web message encoding 基本上,您必须覆盖默认的Web消息编码

Create a custom mapper: 创建一个自定义映射器:

public class RawContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        if (contentType.Contains("text/xml") || contentType.Contains("application/xml"))
        {
            return WebContentFormat.Raw;
        }
        else
        {
            return WebContentFormat.Default;
        }
    }
}

Then refer to it in the binding configuration: 然后在绑定配置中引用它:

<service behaviorConfiguration="NotificationServiceBehavior" name="AtlanticGateway.GatewayComponent.Services.NotificationService">

<endpoint behaviorConfiguration="web" binding="customBinding" bindingConfiguration="RawReceiveCapable" contract="AtlanticGateway.GatewayComponent.Message.INotificationService" />


<bindings>
 <customBinding>
   <binding name="RawReceiveCapable">

     <webMessageEncoding  webContentTypeMapperType="AtlanticGateway.GatewayComponent.RawContentTypeMapper, AtlanticGateway.GatewayComponent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

     <httpTransport manualAddressing="true" maxReceivedMessageSize="524288000" transferMode="Streamed" />
     ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM