WCF三种通讯模式

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

请求与答复模式web

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

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

[OperationContract]
string ShowName(string name);

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

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

优势:有返回值咱们就能够向客户端返回错误信息,如:只接收".rar"文件等信息。blog

 

单向模式接口

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

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

[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

相关文章
相关标签/搜索