服务端配置文件

1 <system.serviceModel> 2 <behaviors> 3 <serviceBehaviors> 4 <behavior name="CallBackContractServicebehavior"> 5 <serviceDebug httpHelpPageEnabled="false"/> 6 <serviceMetadata httpGetEnabled="false"/> 7 <serviceTimeouts transactionTimeout="00:10:00"/> 8 <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/> 9 </behavior> 10 </serviceBehaviors> 11 </behaviors> 12 13 <bindings> 14 <netTcpBinding> 15 <binding name="tcpbinding"> 16 <security mode="None"> 17 <transport clientCredentialType="None" protectionLevel="None"/> 18 </security> 19 </binding> 20 </netTcpBinding> 21 </bindings> 22 <services> 23 <service name="MyWCF.CallBack.Service.CallBackContractService" behaviorConfiguration="CallBackContractServicebehavior"> 24 <host> 25 <baseAddresses> 26 <add baseAddress="net.tcp://localhost:12475/CalculatorService"/> 27 </baseAddresses> 28 </host> 29 <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpbinding" contract="MyWCF.CallBack.Interfac.ICallBackContract"/> 30 <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/> 31 </service> 32 </services> 33 <!--<behaviors> 34 <serviceBehaviors> 35 <behavior name="MathServicebehavior"> 36 <serviceDebug httpHelpPageEnabled="false"/> 37 <serviceMetadata httpGetEnabled="false"/> 38 <serviceTimeouts transactionTimeout="00:10:00"/> 39 <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/> 40 </behavior> 41 </serviceBehaviors> 42 </behaviors> 43 <bindings> 44 <basicHttpBinding> 45 <binding name="httpbinding"/> 46 </basicHttpBinding> 47 </bindings> 48 <services> 49 <service name="SOA.WCF.Service.MathService" behaviorConfiguration="MathServicebehavior"> 50 <host> 51 <baseAddresses> 52 <add baseAddress="http://localhost:11113/MathService"/> 53 </baseAddresses> 54 </host> 55 <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpbinding" contract="SOA.WCF.Interface.IMathService"/> 56 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 57 </service> 58 </services>--> 59 60 </system.serviceModel>
服务端双工协议执行类接口注明回调约束ICallBack
[ServiceContract(CallbackContract =typeof(ICallBack))] public interface ICallBackContract { [OperationContract] int Plus(int x,int y); }
服务端设置回掉类库

public interface ICallBack { /// <summary> /// 回调 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="iResult"></param> [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<ICallBack>(); 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(); }