0

我一直在努力研究WCF Json-Rpc Service Model。我对 WCF 和 WCF 可扩展性相当陌生,但最后我现在能够处理来自 Web 浏览器的请求 :) 总而言之,我现在已经实现了一个端点行为、一个操作选择器和一个消息格式化程序。

您可以在 MSDN 论坛上的这篇文章中找到最新的源代码。

我现在正在尝试为它创建一个 WCF 客户端,但我遇到了以下错误:

Manual addressing is enabled on this factory, so all messages sent must be pre-addressed.

这就是我创建客户的方式:

    private int communicationTimeout = 10;
    private int communicationPort = 80;
    private string jsonRpcRoot = "/json.rpc";

    public void InitializeClient()
    { 
        Uri baseAddress = new UriBuilder(Uri.UriSchemeHttp, Environment.MachineName, communicationPort, jsonRpcRoot).Uri;
        EndpointAddress address = new EndpointAddress(baseAddress.AbsoluteUri);

        ChannelFactory<IJsonService> channelFactory = new ChannelFactory<IJsonService>(new WebHttpBinding(), address);
        channelFactory.Endpoint.Behaviors.Add(new JsonRpcEndpointBehavior());

        IJsonService typedProxy = channelFactory.CreateChannel();

        int a = typedProxy.StartTransport(10);
    }

这是我的(测试)服务合同。我尽可能简单

[ServiceContract(Namespace = "")]
public interface IJsonService
{
    [OperationContract]
    IList<Mission> GetMissions();

    [OperationContract]
    int StartTransport(int missionId);

    [OperationContract]
    int TransportCompleted(int missionId);
}
4

1 回答 1

1

我的问题的答案在于消息格式化程序。

该错误表明构建并返回到 WCF 堆栈的消息不包含地址。

为了满足这个规则, IClientMessageFormatter 应该包含一些值

Message.Headers.To

我已将 IClientMessageFormatter.SerializeRequest 实现更改如下:

    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;
    }
于 2013-09-26T14:27:34.810 回答