我一直在努力WCF Json-Rpc Service Model。 我对WCF和WCF的可扩展性还不熟悉,但最后我现在能够处理来自Web浏览器的请求:)总结一下,我现在已经实现了一个端点行为,一个操作选择器和一个消息格式化器。
您可以在此post on MSDN forum找到最新的源代码。
我现在正在尝试为它创建一个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);
}
答案 0 :(得分:0)
我的问题的答案就在于消息格式化程序。
该错误表示构建并返回到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;
}