• WCF三种通信模式


    WCF在通信过程中有三种模式:请求与答复、单向、双工通信。

    请求与答复模式

    描述:客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务端有了答复后才能继续执行其他程序

    请求与答复模式为WCF的默认模式,如下代码所示:

    [OperationContract]
    string ShowName(string name);
    

    即使返回值是void 也属于请求与答复模式。

    缺点:如果用WCF在程序A中上传一个2G的文件,那么要想执行程序B也许就是几个小时后的事情了。如果操作需要很长的时间,那么客户端程序的响应能力将会大大的下降。

    优点:有返回值我们就可以向客户端返回错误信息,如:只接收".rar"文件等信息。

    单向模式

    描述:客户端向服务端发送求,但是不管服务端是否执行完成就接着执行下面的程序。

    单向模式要在OpertaionContract的属性中显示设置值,代码如下:

    [OperationContract(IsOneWay = true)]
    void ShowName(string name);
    

    优缺点与“请求响应模式”差不多倒过来。

    特点:使用 IsOneWay=true 标记的操作不得声明输出参数、引用参数或返回值。

    双工模式

    描述:双工模式建立在答复模式和单向模式的基础之上,实现客户端与服务端相互的调用。

    相互调用:以往我们只是在客户端调用服务端,然后服务端有返回值返回客户端,而相互调用不光是客户端调用服务端,而且服务端也可以调用客户端的方法。

    1.添加WCF服务 Service2.svc,并定义好回调的接口,服务器端接口IService2.cs:

    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

    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为例:

    <endpoint address=""  binding="wsDualHttpBinding" contract="WcfService1.IService2"></endpoint>
    <system.serviceModel>
        <services>
          <service name="WcfService1.Service2">
            <endpoint address=""  binding="wsDualHttpBinding" contract="WcfService1.IService2"></endpoint>
          </service>
        </services>
    </system.serviceModel>
    

      

    4.客户端调用

    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

  • 相关阅读:
    重塑矩阵
    买卖股票
    两个数组的交集
    洛谷 P3700
    HDU 6987
    Solution -「NOI 2021」「洛谷 P7740」机器人游戏
    「NOI 2021」酱油记
    Solution -「UNR #5」「UOJ #671」诡异操作
    CD from Codeforces Round #703 (Div. 2)
    CD from Codeforces Round #701 (Div. 2)
  • 原文地址:https://www.cnblogs.com/xsj1989/p/5729621.html
Copyright © 2020-2023  润新知