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

查看:63
本文介绍了WCF输入巨大的XML作为Content-Type的流:xml/text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESTful WCF Web服务,该服务处理巨大的XML文件,这些文件作为流以Header Content-Type:使用POST方法的文本/文本作为流传递.当客户端尝试将此Web服务与Header Content-Type:text/xml一起使用时,他们会收到"...包含无法识别的http正文格式值'Xml'.预期的正文格式值为'Raw'.这可以原因是尚未在绑定上配置WebContentTypeMapper"错误.我的任务是使此Web服务与Header Content-Type:text/xml一起使用,因为许多客户端将此Web服务与其他服务一起使用,并且不想仅更改此服务的内容类型.如何将传入的Stream映射为WebContentFormat.Raw并获取此Web服务以接受Content-Type:text/xml?谢谢.

I have a RESTful WCF web service that processes huge XML files that are passed in as a Stream with a Header Content-Type: text/text using a POST method. When a client tries to use this web service with a Header Content-Type: text/xml, they receive a "...contains an unrecognized http body format value 'Xml'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding" error. I am tasked with making this web service work with a Header Content-Type:text/xml as a multitude of clients use this web services with other services and do not want to change the content type just for this service. How do I map the incoming Stream as WebContentFormat.Raw and get this web service to accept the Content-Type:text/xml? Thank you.

推荐答案

我解决了此问题,方法是创建一个新类,该类从WebContentTypeMapper派生,当Content-Type ='text/xml'时,将WebContentFormat更改为'Raw'.与这个新类一起,我更新了web.config以在"bindings"下使用"customBinding"元素.

I solved this issue by creating a new class that derives from WebContentTypeMapper and changing the WebContentFormat to 'Raw' when the Content-Type = 'text/xml'. Along with this new class, I updated the web.config to use the 'customBinding' element under 'bindings'.

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

web.config

web.config

<bindings>
  <customBinding>
    <binding name="XmlMapper">
      <webMessageEncoding webContentTypeMapperType="Lt.Trigger.XmlContentTypeMapper, ExService" />
      <httpTransport manualAddressing="true" />
    </binding>
  </customBinding>
</bindings>

这篇关于WCF输入巨大的XML作为Content-Type的流:xml/text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆