Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

c# - WCF input huge XML as Stream with Content-Type: xml/text

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...