2

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

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

WCF输入巨大的XML作为内容类型的流:xml/text

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

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

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

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

合同

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

服务

public void IncomingXML(Stream xml)
{
}

网络配置:

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

1 回答 1

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" />
     ...
于 2016-12-20T08:54:27.713 回答