I'm working with WCF Extensibilty and I've created a IClientMessageFormatter that serialize requests via Json-RPC. The code is this one:
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
string jsonText = SerializeJsonRequestParameters(parameters);
// Compose message
Message message = Message.CreateMessage(messageVersion, _clientOperation.Action, new JsonRpcBodyWriter(Encoding.UTF8.GetBytes(jsonText)));
message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
_address.ApplyTo(message);
HttpRequestMessageProperty reqProp = new HttpRequestMessageProperty();
reqProp.Headers[HttpRequestHeader.ContentType] = "application/json";
message.Properties.Add(HttpRequestMessageProperty.Name, reqProp);
UriBuilder builder = new UriBuilder(message.Headers.To);
builder.Query = string.Format("jsonrpc={0}", HttpUtility.UrlEncode(jsonText));
message.Headers.To = builder.Uri;
message.Properties.Via = builder.Uri;
return message;
}
I was trying to use HttpRequestMessageProperty to force WCF to use GET http verb, but setting the following:
reqProp.Method = "GET";
cause WCF to throw a System.Net.ProtocolViolationException. Can anyone tell me what I'm doing wrong?
Found the answer myself! The GET verb requires the Message to contains no body:
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
{
string jsonText = SerializeJsonRequestParameters(parameters);
// Compose message
Message message = Message.CreateMessage(messageVersion, _clientOperation.Action);
_address.ApplyTo(message);
HttpRequestMessageProperty reqProp = new HttpRequestMessageProperty();
reqProp.Headers[HttpRequestHeader.ContentType] = "application/json";
reqProp.Method = "GET";
message.Properties.Add(HttpRequestMessageProperty.Name, reqProp);
UriBuilder builder = new UriBuilder(message.Headers.To);
builder.Query = string.Format("jsonrpc={0}", HttpUtility.UrlEncode(jsonText));
message.Headers.To = builder.Uri;
message.Properties.Via = builder.Uri;
return message;
}