WCF returns HTTP 400, but only when the Content-Type parameter is set to "text / xml" or "text / json",

I'm really at a standstill. I tried every tool I can imagine, checked every resource that I can and still cannot understand.

I have a WCF service designed to handle simple old XML requests in a proprietary XML format (this is an old ASMX conversion). I configured the service using WCF 3.5 to accept simple HTTP POST requests, and it works fine until the Content-Type in the request header is set to "text / xml" (or any other type of XML or JSON, such as "text / json "or" application / json ").

In other words, if I set the request content type to “text” or “text / plain” or even “whatever”, the service works as expected. However, when I set the content type to "text / xml", the service failed with an HTTP 400 Bad Request.

And I could not find a way to debug the refusal to receive additional information. The actual service method is never called, and the IErrorHandler implementation also does not detect errors. I suspect that something is wrong with my WCF configuration or with my IIS 7 configuration, but I absolutely do not know what is happening.

Here is my service contract:

using System.ServiceModel; using System.ServiceModel.Web; using System.IO; namespace App.api.v_1_1 { [ServiceContract] public interface IPlainXmlWebServiceViaHttpPost { [WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] [OperationContract] Stream Process(Stream xml); } } 

And here is my service implementation:

 using System.IO; using System.ServiceModel.Activation; using App.api.v_1_0; using System.ServiceModel; namespace App.api.v_1_1 { [ServiceBehavior(Namespace = Constants.WebServiceNamespace)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] public class PlainXmlWebServiceViaHttpPost : IPlainXmlWebServiceViaHttpPost { public Stream Process(Stream request) { return request.ProcessTextStreamWith(PoxProcessor.Process); } } } 

Basically, it just turns the incoming stream into a string, does something with that string and returns the string response converted back to Stream. The idea is to be able to pass arbitrary XML to the service and return arbitrary XML.

And finally, here is the relevant part of web.config:

 <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <bindings> <webHttpBinding> <binding name="poxBinding" maxReceivedMessageSize="655360"> <readerQuotas maxArrayLength="655360" /> <security mode="None"> <transport clientCredentialType="None" /> </security> </binding> </webHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="REST"> <webHttp /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="poxBehavior"> <serviceDebug httpHelpPageEnabled="false" includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="poxBehavior" name="App.api.v_1_1.PlainXmlWebServiceViaHttpPost"> <endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding" bindingConfiguration="poxBinding" contract="App.api.v_1_1.IPlainXmlWebServiceViaHttpPost" /> </service> </services> </system.serviceModel> 

I tried all kinds of workarounds to get WCF / IIS to work with me, and everyone failed. Does anyone know what will cause the requested content types to return an HTTP 400, but all other content types will be processed as expected?

+4
source share
2 answers

You used WebServiceHostFactory in the .svc file:

 <%@ ServiceHost Factory="System.ServiceModel.Activation.WebServiceHostFactory" Service="App.api.v_1_1.PlainXmlWebServiceViaHttpPost" %> 
+7
source

A few thoughts for you. You can take a look at the leisure relaxation kit . They created a bunch of additional materials to help create calm services and have tons of great examples.

Another thing to try when working with wcf services is to enable message logging. You can do this in your configuration file as follows:

 <system.serviceModel> <diagnostics> <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" /> </diagnostics> 

Log files can be read using SvcTraceViewer.exe (My is here: C: \ Program Files \ Microsoft SDK \ Windows \ v6.0A \ bin). I believe this tool comes with a Windows SDK. The configuration editor (SvcConfigEditor.exe) found in the same place can be used to edit your web.config. There are other options for logging if you click on the Diagnostics folder after loading the configuration file into the editor.

+2
source

All Articles