Hi, i have a Java mobile clients to my service. Some phones modify
Content-Type of request by inserting text/plain before
application/json; charset=utf-8. This causes exception on the server
because wcf REST service treates incomming request as raw data and uses
stream encoder. I need to replace this with JsonMessageEncoder , for
this i wrote an interceptor and able to catch requests, but i am getting the following error:
---------------------------------------------------------------------------------------
Error Status Code: 'InternalServerError'
Details: Unable to deserialize XML body with root name 'Binary' and
root namespace '' (for operation 'Login' and contract ('Auth',
'http://tempuri.org/')) using DataContractSerializer. Ensure that the type
corresponding to the XML is added to the known types collection of the
service.
---------------------------------------------------------------------------------------
Here is my interceptor:
public class MessageInterceptor : RequestInterceptor
{
public MessageInterceptor(): base(false)
{
}
public override void ProcessRequest(ref RequestContext requestContext)
{
if (requestContext == null) return;
Message request = requestContext.RequestMessage;
if (request == null) return;
var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
string contentType = prop.Headers[HttpRequestHeader.ContentType];
if (contentType != null)
{
if (contentType.Contains("text/plain"))
{
prop.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";
request.Properties[HttpRequestMessageProperty.Name] = prop;
request.Properties[WebBodyFormatMessageProperty.Name] = new WebBodyFormatMessageProperty(WebContentFormat.Json);
//var encoder = (MessageEncoder)request.Properties["Encoder"];
request.Properties.Remove("Encoder");
var element = new WebMessageEncodingBindingElement();
var encFac = element.CreateMessageEncoderFactory();
request.Properties.Add("Encoder",encFac.Encoder);
}
}
}
}