Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包含无法识别的http正文格式值';Xml';_C#_Xml_Wcf_Web Config_Wcf Configuration - Fatal编程技术网

C# 包含无法识别的http正文格式值';Xml';

C# 包含无法识别的http正文格式值';Xml';,c#,xml,wcf,web-config,wcf-configuration,C#,Xml,Wcf,Web Config,Wcf Configuration,关于堆栈溢出有许多类似的问题,但没有一个能帮助我制定出解决方案 下面的问题是最接近的,但我无法让它工作,当我尝试实现它时,我的其他服务停止工作 错误:操作“IncomingXML”(具有命名空间的协定…)的传入消息包含无法识别的http正文格式值“Xml”。预期的正文格式值为“原始”。这可能是因为尚未在绑定上配置WebContentTypeMapper。有关更多详细信息,请参阅WebContentTypeMapper的文档 基本上,我需要这个并且只有这个web服务才能接受“applicatio

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

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

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

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

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

有人有什么建议吗?如果我无法在不中断其他web服务的情况下实现此功能

合同

[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>

...
...

这篇博文提供了解决方案:

基本上,您必须覆盖默认的web消息编码

创建自定义映射器:

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

...

这篇博文提供了解决方案:

基本上,您必须覆盖默认的web消息编码

创建自定义映射器:

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

...