我正在使用WCF Extensibilty,我创建了一个IClientMessageFormatter来通过Json-RPC序列化请求。代码就是这个:
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;
}
我试图使用HttpRequestMessageProperty强制WCF使用GET http动词,但设置如下:
reqProp.Method = "GET";
导致WCF抛出System.Net.ProtocolViolationException。 谁能告诉我我做错了什么?
答案 0 :(得分:2)
自己找到答案! GET动词要求消息不包含正文:
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;
}