I've configured a RESTful WCF with the following POST "operation":
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Test", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json)]
void PostTest(Stream stream);
In my web.config, I've configured the following:
<service name="MyTest.TestSvc" behaviorConfiguration="MyTest.TestBehavior" >
<endpoint address="" behaviorConfiguration="MyBehavior" binding="webHttpBinding" contract="MyTest.ITestSvc"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<endpointBehaviors>
<behavior name="MyBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyTest.TestBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
When I sent a POST message using "text/plain" or "json" everything works fine. However, when I try to send a POST message with ContentType = "application/json" It fails with the following message: The remote server returned an error: (400) Bad Request
The only solution which I found was to define the Factory class : System.ServiceModel.Activation.WebServiceHostFactory int the Svc definition markup.
I found this solution in the following link: Send JSON to WCF 3.5 using Ajax
As I understood , defining the WebServiceHostFactory is only useful if you don't want to edit the web.config.
How can I make it work without defining the WebServiceHostFactory?
Notice that I succeed to get "json" content type POST message but not "application/json" content type.
The problem is that to use the raw programming model (using a
Stream
parameter), you'll need to tell WCF to not try to understand the request as JSON, instead just pass the raw request body to the parameter. You can do that using aWebContentTypeMapper
. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx shows how this can be done. The solution with the factory works because it does that when it creates the endpoint.