WCF在通信过程中有三种模式:请求与答复、单向、双工通信。
请求与答复模式
描述:客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务端有了答复后才能继续执行其他程序
请求与答复模式为WCF的默认模式,如下代码所示:
1 2 | [OperationContract] string ShowName( string name); |
即使返回值是void 也属于请求与答复模式。
缺点:如果用WCF在程序A中上传一个2G的文件,那么要想执行程序B也许就是几个小时后的事情了。如果操作需要很长的时间,那么客户端程序的响应能力将会大大的下降。
优点:有返回值我们就可以向客户端返回错误信息,如:只接收".rar"文件等信息。
单向模式
描述:客户端向服务端发送求,但是不管服务端是否执行完成就接着执行下面的程序。
单向模式要在OpertaionContract的属性中显示设置值,代码如下:
1 2 | [OperationContract(IsOneWay = true )] void ShowName( string name); |
优缺点与“请求响应模式”差不多倒过来。
特点:使用 IsOneWay=true 标记的操作不得声明输出参数、引用参数或返回值。
双工模式
描述:双工模式建立在答复模式和单向模式的基础之上,实现客户端与服务端相互的调用。
相互调用:以往我们只是在客户端调用服务端,然后服务端有返回值返回客户端,而相互调用不光是客户端调用服务端,而且服务端也可以调用客户端的方法。
1.添加WCF服务 Service2.svc,并定义好回调的接口,服务器端接口IService2.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService1 { //CallbackContract = typeof(IService2CallBack) 定义回调的接口类型 [ServiceContract(CallbackContract = typeof (IService2CallBack))] public interface IService2 { [OperationContract] string ShowName( string name); } //回调的接口,该接口中的方法在客户端实现 public interface IService2CallBack { //IsOneWay = true 启动单向模式,该模式方法不能有返回值 [OperationContract(IsOneWay = true )] void PrintSomething( string str); } } |
2.服务端实现 Service2.svc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfService1 { public class Service2 : IService2 { IService2CallBack callback = null ; //回调接口类型 public Service2() { //获取调用当前操作的客户端实例 callback = OperationContext.Current.GetCallbackChannel<IService2CallBack>(); } /// <summary> /// 被客户端调用的服务 /// </summary> /// <param name="name"></param> /// <returns></returns> public string ShowName( string name) { callback.PrintSomething(name); return "服务器调用客户端,WCF服务,显示名称:xsj..." ; } } } |
3.服务器端配置,web.config 在system.serviceModel中添加配置,
支持回调的绑定有4种:WSDualHttpBinding、NetTcpBinding、NetNamedPipeBinding、NetPeerTcpBinding。
我们这里用WSDualHttpBinding为例:
1 | <endpoint address= "" binding= "wsDualHttpBinding" contract= "WcfService1.IService2" ></endpoint> |
1 2 3 4 5 6 7 | <system.serviceModel> <services> <service name= "WcfService1.Service2" > <endpoint address= "" binding= "wsDualHttpBinding" contract= "WcfService1.IService2" ></endpoint> </service> </services> </system.serviceModel> |
4.客户端调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Program { static void Main( string [] args) { InstanceContext instanceContext = new InstanceContext( new CallbackHandler()); Service2Client client = new Service2Client(instanceContext); string result = client.ShowName( "jay.xing" ); Console.WriteLine(result); Console.ReadLine(); } } //实现服务端的回调接口 public class CallbackHandler : IService2Callback { public void PrintSomething( string str) { Console.WriteLine( "test data:" + str); } } |
介绍结束,参考:http://www.cnblogs.com/iamlilinfeng/archive/2012/10/03/2710698.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】