服务端配置文件
12 3 12 134 115 106 7 8 9 14 2215 2116 2017 1918 23 33 59 6024 3225 2926 2827 30 31
服务端双工协议执行类接口注明回调约束ICallBack
[ServiceContract(CallbackContract =typeof(ICallBack))] public interface ICallBackContract { [OperationContract] int Plus(int x,int y); }
服务端设置回掉类库
public interface ICallBack { ////// 回调 /// /// /// /// [OperationContract(IsOneWay =true)] void Show(int x,int y,int iResult); }
服务端实现方法
public class CallBackContractService : ICallBackContract { public int Plus(int x, int y) { int result = x + y; Thread.Sleep(2000); ICallBack callBack = OperationContext.Current.GetCallbackChannel(); callBack.Show(x, y, result); return result; } }
客服端实现调用继承服务端的实现类库
public class CallBackService : MyCallBack.ICallBackContractCallback { public void Show(int x, int y, int iResult) { Console.WriteLine($"回调显示:{x}+{y}={iResult}"); } }
客户端调用代码
static void Main(string[] args) { MyCallBack.CallBackContractClient client = null; try { InstanceContext instanceContext = new InstanceContext(new CallBackService()); client = new MyCallBack.CallBackContractClient(instanceContext); int a=client.Plus(1, 2); client.Close(); } catch (Exception ex) { if (client != null) client.Abort(); Console.WriteLine(ex); } Console.Read(); }