wcfInterface 项目
》》》wcf服务端提供的服务协议
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace wcfInterface
{[ServiceContract(CallbackContract= typeof(ICallBack))]public interface ICalculatorService{[OperationContract(IsOneWay =true)]void Minus(int x, int y);}
}
》》》 回调接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace wcfInterface
{/// <summary>/// 这个是约束,不需要协议,有客户端实现/// </summary>public interface ICallBack{[OperationContract(IsOneWay = true)]void Result(int x, int y, int result);}
}
wcfService 实现wcfInterface
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wcfInterface;namespace wcfService
{public class CalculatorService : ICalculatorService{/// <summary>/// 完成计算,然后返回值/// 客户端请问服务器,服务端处理完成之后,触发客户端方法/// </summary>/// <param name="x"></param>/// <param name="y"></param>public void Minus(int x, int y){int result = x - y;// 模拟服务端费时操作Thread.Sleep(3000);//OperationContext 操作上下文 ,客户端请求 服务端,会携带回调信息的ICallBack callback = OperationContext.Current.GetCallbackChannel<ICallBack>();callback.Result(x, y, result);}}
}
宿主到控制台
支持双工的 协议 tcp、Duplehttp
》》》 config配置文件
<system.serviceModel><bindings><netTcpBinding><binding name="tcpbinding" ><security mode="None"><transport clientCredentialType="None" protectionLevel="None"></transport></security></binding></netTcpBinding></bindings><services><service name="wcfService.CalculatorService" behaviorConfiguration="behaviorConfiguration"><!--服务的对象--><host><baseAddresses><add baseAddress="net.tcp://localhost:8000/CalculatorService"/><!--服务的IP和端口号--></baseAddresses></host><endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpbinding" contract="wcfInterface.ICalculatorService"></endpoint><endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint><!--contract:服务契约--></service></services><behaviors><serviceBehaviors><behavior name="behaviorConfiguration"><serviceDebug httpHelpPageEnabled="false"/><serviceMetadata httpGetEnabled="false"/><serviceTimeouts transactionTimeout="00:10:00"/><serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/></behavior></serviceBehaviors></behaviors></system.serviceModel>
》》》 启动服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using wcfInterface;
using wcfService;
namespace wcfConsole
{class Program{static void Main(string[] args){//用列表 可以方便添加多个服务挂载List<ServiceHost> hosts = new List<ServiceHost>() {new ServiceHost(typeof(CalculatorService))};foreach (var host in hosts){host.Opening += delegate {Console.WriteLine($"{host.GetType()}服务正在启动中。。。。");};host.Opened += (o, e) =>{Console.WriteLine($"{host.GetType()}服务 启动成功啦!");};host.Open();}Console.WriteLine("按任意键,终止服务");Console.Read();foreach (var host in hosts){host.Close();}Console.Read();}}
}
》》》》 以上 是服务端的代码
》》》》以下是客户端代码
客户端代码
》》》引用服务
》》》 新建一个类 CallBackService 继承 上面的服务中的 回调接口 zenCalculator.ICalculatorServiceCallback
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{public class CallBackService : zenCalculator.ICalculatorServiceCallback{public void Result(int x, int y, int result){Console.WriteLine($"回调操作结果:{x}-{y},{result}");}}
}
》》》 以控制台 为了 请问wcf服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.SelfHost;namespace ConsoleApp1
{class Program{static void Main(string[] args){zenCalculator.CalculatorServiceClient client = null;try{Console.WriteLine("===============================");//双工要借助 InstanceContext new InstanceContext( 上面新建类对象 )InstanceContext context = new InstanceContext(new CallBackService() );client = new zenCalculator.CalculatorServiceClient(context);client.Minus(55, 3);client.Close();}catch (Exception ex){if (client!=null){client.Abort();}throw;}Console.Read();}}
}