堆栈溢出有很多类似的问题,但没有一个能帮助我制定解决方案。
下面的问题是最接近的,但是当我尝试实现它时,我无法让它工作,我的其他服务停止工作。
WCF input huge XML as Stream with Content-Type: xml/text
错误:操作'IncomingXML'的传入消息(契约...与命名空间...)包含无法识别的http正文格式值'Xml'。预期的正文格式值为“Raw”。这可能是因为尚未在绑定上配置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>
答案 0 :(得分:0)
此博文有解决方案: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" />
...