wcf双工通信
一直以為感覺雙工沒弄懂,著實覺得很惆悵,在網上了解下雙工的一些特點,直接上代碼,以便以后項目中用的著:
service層:
定義一個IDuplexHello服務接口 [ServiceContract(Name = "DuplexHello",Namespace = "http://microsoft.wcf.documentation",CallbackContract = typeof(IHelloCallbackContract), //設置回調服務類型SessionMode = SessionMode.Required)]public interface IDuplexHello{[OperationContract]void Hello(string greeting);} 實現DuplexHello.cspublic class DuplexHello : IDuplexHello{ public void Hello(string greeting){Console.WriteLine("Caller sent: " + greeting);Console.WriteLine("Session ID: " + OperationContext.Current.SessionId);Console.WriteLine("Waiting two seconds before returning call.");Thread.Sleep(2000);var callerProxy= OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();var response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;Console.WriteLine("Sending back: " + response);callerProxy.Reply(response);}} 定義回調接口(回調服務函數沒有必要添加servicecontract,因為人家是包含在IDuplexHello服務類型中的) public interface IHelloCallbackContract{[OperationContract(IsOneWay = true)] //一定要記得添加operationcontractvoid Reply(string responseToGreeting);} 這里的回調接口實現類(HelloCallbackContract.cs)我是直接在service層實現的,當然在項目里面肯能大多數情況下都是在客服端實現,我這里也就為了方便啦public class HelloCallbackContract : IHelloCallbackContract{public void Reply(string responseToGreeting){Console.WriteLine(responseToGreeting);}}將service寄宿到控制臺應用程序中(用的都是代碼實現):
private static void Main(string[] args){using (var host = new ServiceHost(typeof (DuplexHello),new Uri("http://localhost:991/DuplexHello"))) //添加基地址,在client就是這個地址,另外端口號不能魚雙工端口號相同{host.AddServiceEndpoint(typeof (IDuplexHello),new NetTcpBinding(),"net.tcp://localhost:999/DuplexHello"); var metadatbehavior =host.Description.Behaviors.Find<ServiceMetadataBehavior>();if (metadatbehavior == null){metadatbehavior = new ServiceMetadataBehavior(){HttpGetEnabled = true};host.Description.Behaviors.Add(metadatbehavior);}host.Opened += delegate{Console.WriteLine("服務已經啟動");};host.Open();Console.Read();}client層使用的也是一個控制臺應用程序:
首先要運行宿主(找到host層資源文件夾bin->debug ?:host.exe)在更新wcf服務的時候也必須要先運行host.exe,否則會出現無法連接到服務器錯誤,
然后添加服務引用中地址欄輸入http://localhost:991/DuplexHello 應用服務
再后應用之后客戶端app.config自動生成如下代碼:
<configuration><system.serviceModel><bindings><netTcpBinding><binding name="NetTcpBinding_DuplexHello" /></netTcpBinding></bindings><client><endpoint address="net.tcp://localhost:999/DuplexHello" binding="netTcpBinding"bindingConfiguration="NetTcpBinding_DuplexHello"contract="ServiceReference1.DuplexHello"name="NetTcpBinding_DuplexHello"><identity><userPrincipalName value="objectboy-PC\objectboy" /></identity></endpoint></client></system.serviceModel> </configuration>客服端控制臺應用程序中代碼:
private static void Main(string[] args){var hellocallbackcontract =new HelloCallbackContract();var instanceContext =new InstanceContext(hellocallbackcontract); //實例化客服端類服務上下文var duplexChannelFactory =new DuplexChannelFactory<IDuplexHello>(instanceContext,new NetTcpBinding()); //實例化雙工通道,并綁定為tcp通信,注意不能用ChannelFactory,因為這是雙工var endpointaddress =new EndpointAddress("net.tcp://localhost:999/DuplexHello");var proxy =duplexChannelFactory.CreateChannel(endpointaddress); //創建并將消息發送到指定的消息通道using (proxy as IDisposable){proxy.Hello("cccccccccccccccccc");}Console.Read();}?客服端輸出:
服務端輸出:
轉載于:https://www.cnblogs.com/objectboy/p/3784218.html
總結
- 上一篇: 知乎夜间模式在哪里打开
- 下一篇: 知乎消息数字提醒哪里关