最近项目使用到点WCF功能,路由服务,重要还是配置文件App.config
Routing服务代码
using System.Text;using System.ServiceModel;using System.ServiceModel.Routing;namespace RoutingServer{ class RoutingServer { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(RoutingService))) { host.Opened = (s, e) => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Routing..."); }; host.Open(); Console.ReadKey(); } } }}
Routing中的App.config
8002端口中代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace ServerA{ class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(AServer))) { host.Opened = (s, e) => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("server A..."); }; host.Open(); Console.ReadKey(); } } } [ServiceContract(SessionMode= SessionMode.Required)] public interface IAServer { [OperationContract] string Send(); } public class AServer : IAServer { public string Send() { return "Hello world"; } }}
backupList中代码跟这8002端口代码一样。
使用Svcutil.exe生成客户端代码
svcutil.exe /out:c:\Routing.cs /config:C:\app.config
svcutil.exe /out:C:\Test.cs /config:c:\app2.config
然后复制app.config跟Test.cs到客户端调用工程里。
客户端演示
一定要修改App.config里的
<endpoint address="net.tcp://192.168.1.200:9002/V1/Routing/DB" binding="netTcpBinding"
bindingConfiguration="DuplexSessionRouter" contract="IAServer" name="DB" />