1

我正在使用 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。谁能告诉我我做错了什么?

4

1 回答 1

2

自己找到了答案!GET 动词要求 Message 不包含正文:

    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;
    }
于 2013-09-26T10:20:14.187 回答