【问题标题】:Contains an unrecognized http body format value 'Xml'包含无法识别的 http 正文格式值“Xml”
【发布时间】:2015-10-23 18:29:57
【问题描述】:

关于堆栈溢出有很多类似的问题,但没有一个帮助我制定解决方案。

下面的问题是最接近的,但我无法让它工作,当我尝试实现它时我的其他服务停止工作。

WCF input huge XML as Stream with Content-Type: xml/text

错误:操作“IncomingXML”的传入消息(带有命名空间的合同...)包含无法识别的 http 正文格式值“Xml”。预期的正文格式值为“原始”。这可能是因为尚未在绑定上配置 WebContentTypeMapper。详情请参阅 WebContentTypeMapper 的文档。

基本上我需要这个并且只有这个 Web 服务才能接受“application/xml”。目前它只接受“raw”,当调用设置了“application/xml”时服务会失败。

问题是两部分,一是允许服务接受 xml,二是不影响其他 Web 服务。这是唯一接受传入 xml 的服务。

有人有什么建议吗?如果我不能在不中断其他网络服务的情况下让它工作。

合同

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

服务

public void IncomingXML(Stream xml)
{
}

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>

【问题讨论】:

    标签: c# xml wcf web-config wcf-configuration


    【解决方案1】:

    这篇博文有解决办法:http://kimcuhoang.blogspot.be/2011/04/solving-raw-issue-with-wcf-and-content_13.html

    基本上你必须覆盖默认的网络消息编码

    创建自定义映射器:

    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;
            }
        }
    }
    

    然后在绑定配置中引用它:

    <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" />
         ...
    

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      相关资源
      最近更新 更多