实现路由到我的wcf服务

问题描述:

我有客户端上传文件到我的服务器使用流媒体wcf服务。客户端上的代码是这样的(忽略了一些细节):实现路由到我的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)); 
} 

我很困惑我怎样才能将我的客户端连接到路由服务。这是一个好方法吗?谢谢。

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