WCF introduces huge XML as a stream with Content-Type: xml / text

I have a RESTful WCF web service that processes huge XML files that are transferred as a stream with the text / text of the Content-Type: text / text header using the POST method. When a client tries to use this web service with the Content-Type: text / xml heading, they get "... contains an unrecognized XML object format value of" Xml ". The expected value of the body format is" Raw "because WebContentTypeMapper was not configured I’m instructed to create this web service with the heading Content-Type: text / xml, since many clients use these web services with other services and do not want to change the content type only for this service.How to map the incoming stream as WebContentFormat .Raw and force this web service to accept Content-Type: text / xml? Thanks.

+4
source share
1 answer

I solved this problem by creating a new class that comes from WebContentTypeMapper and changing the value of WebContentFormat to "Raw" when Content-Type = "text / xml". Along with this new class, I updated web.config to use the customBinding element in the "bindings" section.

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> 
+5
source

All Articles