WCF的ChannelFactory使用自定义端点行为(JSON-RPC) [英] WCF ChannelFactory with Custom Endpoint Behavior (Json-Rpc)

查看:155
本文介绍了WCF的ChannelFactory使用自定义端点行为(JSON-RPC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很努力在 WCF JSON-RPC服务模型 。 我是相当新的WCF和WCF的可扩展性,但​​最后我现在可以处理从Web浏览器:)总结一下它的请求,我现在已经实现了一个终结点行为,操作选择和消息格式。

您可以找到关于这个最新的源$ C ​​$ C <一个href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/b7e13640-8a2c-47e0-aae8-79dc413d5635/implement-jsonrpc-with-wcf-over-http#b7e13640-8a2c-47e0-aae8-79dc413d5635"相对=nofollow>邮递MSDN论坛。

我现在想要创建一个WCF客户端,但我坚持,错误如下:

 手动解决这一出厂时已启用,所以派必须是$ P $的所有消息对解决。
 

这是我如何创建我的客户:

 私人INT communicationTimeout = 10;
    私人诠释communicationPort = 80;
    私人字符串jsonRpcRoot =/json.rpc;

    公共无效InitializeClient()
    {
        乌里baseAddress =新UriBuilder(Uri.UriSchemeHttp,Environment.MachineName,communicationPort,jsonRpcRoot).Uri;
        的EndpointAddress地址=新的EndpointAddress(baseAddress.AbsoluteUri);

        的ChannelFactory&LT; IJsonService&GT;的ChannelFactory =新的ChannelFactory&LT; IJsonService&GT;(新的WebHttpBinding(),地址);
        channelFactory.Endpoint.Behaviors.Add(新JsonRpcEndpointBehavior());

        IJsonService typedProxy = channelFactory.CreateChannel();

        INT一个= typedProxy.StartTransport(10);
    }
 

这是我的(测试)的服务合同。我一直是越简单越好

  [的ServiceContract(命名空间=)]
公共接口IJsonService
{
    [OperationContract的]
    IList的&LT;使命&GT; GetMissions();

    [OperationContract的]
    INT StartTransport(INT missionId);

    [OperationContract的]
    INT TransportCompleted(INT missionId);
}
 

解决方案

在回答我的问题是躺在消息格式化。

该错误指示消息建成并返回到WCF栈,不包含地址。

为了满足此规则,IClientMessageFormatter应该包括一些价值

  

Message.Headers.To

我已经改变了IClientMessageFormatter.SerializeRequest实现如下:

 公共信息SerializeRequest(MessageVersion messageVersion,对象[]参数)
    {
        字符串jsonText = SerializeJsonRequestParameters(参数);

        //撰写邮件
        消息消息= Message.CreateMessage(messageVersion,_clientOperation.Action,新JsonRpcBodyWriter(Encoding.UTF8.GetBytes(jsonText)));
        message.Properties.Add(WebBodyFormatMessageProperty.Name,新WebBodyFormatMessageProperty(WebContentFormat.Raw));
        _address.ApplyTo(消息);

        HTT prequestMessageProperty reqProp =新HTT prequestMessageProperty();
        reqProp.Headers [Htt的prequestHeader.ContentType] =应用/ JSON;
        message.Properties.Add(Htt的prequestMessageProperty.Name,reqProp);

        UriBuilder建设者=新UriBuilder(message.Headers.To);
        builder.Query =的String.Format(jsonrpc = {0},HttpUtility.UrlEn code(jsonText));
        message.Headers.To = builder.Uri;
        message.Properties.Via = builder.Uri;

        返回消息;
    }
 

I've been working really hard on a WCF Json-Rpc Service Model. I'm fairly new to WCF and WCF extensibility but finally I'm now able to process requests from a web browser :) To summarize it, I've now implemented an Endpoint Behavior, an Operation Selector, and a Message Formatter.

You can find the latest source code on this post on MSDN forum.

I'm now trying to create a WCF Client for it but I'm stuck with the following error:

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

This is how I'm creating my client:

    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);
    }

And this is my (test) service contract. I kept it as simple as possible

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

    [OperationContract]
    int StartTransport(int missionId);

    [OperationContract]
    int TransportCompleted(int missionId);
}

解决方案

The answer to my question was lying in the message formatter.

The error indicates that the message built and returned to the WCF stack, doesn't contain an address.

In order to satisfy this rule, the IClientMessageFormatter should include some value in

Message.Headers.To

I've changed the IClientMessageFormatter.SerializeRequest implementation as following:

    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;
    }

这篇关于WCF的ChannelFactory使用自定义端点行为(JSON-RPC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆