实现路由到我的wcf服务

时间:2011-06-30 20:03:53

标签: wcf routing

我有客户端使用带流媒体的wcf服务将文件上传到我的服务器。客户端上的代码是这样的(省略一些细节):

NetTcpBinding binding = new NetTcpBinding();
EndpointAddress address = new EndpointAddress("net.tcp://" + ipAddress + ":5000/DataUploader");
ChannelFactory<IDataUploader> channel = new ChannelFactory<IDataUploader>(binding, address);
IDataUploader uploader = channel.CreateChannel();

try
{
    uploader.Upload(msg);
    ConsoleText.Record("The file was sent...\n");
}
catch (CommunicationException)
{
    ConsoleText.Record("The file was not sent...\n" + "Interrupted connection...\n");
}
finally
{
    uploadStream.Close();
    ((IClientChannel)uploader).Close();
}

我想在服务器和客户端之间实现路由服务,路由服务将是这样的:

private static void ConfigureRouterViaCode(ServiceHost serviceHost)
{
    string clientAddress = "http://localhost:5000/DataUploader";
    string routerAddress = "http://localhost:5000/RouterService";

    Binding routerBinding = new WSHttpBinding();
    Binding clientBinding = new WSHttpBinding();

    serviceHost.AddServiceEndpoint(typeof(IRequestReplyRouter), routerBinding, routerAddress);

    ContractDescription contract = ContractDescription.GetContract(typeof(IRequestReplyRouter));
    ServiceEndpoint client = new ServiceEndpoint(contract, clientBinding, new EndpointAddress(clientAddress));

    RoutingConfiguration rc = new RoutingConfiguration();

    List<ServiceEndpoint> endpointList = new List<ServiceEndpoint>();
    endpointList.Add(client);

    rc.FilterTable.Add(new MatchAllMessageFilter(), endpointList);

    serviceHost.Description.Behaviors.Add(new RoutingBehavior(rc));
}

我很困惑如何首先将我的客户端连接到路由服务。这是一个好方法吗?感谢。

1 个答案:

答案 0 :(得分:0)

你的方法是正确的。在客户端上,更改指向路由服务的地址,保留所有其他设置。我建议你学习http://msdn.microsoft.com/en-us/library/ee517423.aspx或者找一些演示路由实现。