【问题标题】:WCF input huge XML as Stream with Content-Type: xml/textWCF输入巨大的XML作为内容类型的流:xml/text
【发布时间】:2010-01-10 21:20:05
【问题描述】:

我有一个 RESTful WCF Web 服务,它处理巨大的 XML 文件,这些文件作为带有 Header Content-Type: text/text 的流传入,使用 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? 谢谢。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    我通过创建一个派生自 WebContentTypeMapper 的新类并在 Content-Type = 'text/xml' 时将 WebContentFormat 更改为 'Raw' 解决了这个问题。除了这个新类,我还更新了 web.config 以使用 'bindings' 下的 'customBinding' 元素。

    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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 2010-11-01
      • 1970-01-01
      • 2014-08-21
      相关资源
      最近更新 更多