WCF通信模式

来源:互联网 发布:c语言对数ln 编辑:程序博客网 时间:2024/06/05 00:11

WCF通信模式

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

请求与答复模式

客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务端有了答复后才能继续执行其他程序,该模式为WCF默认模式。
这里写图片描述
缺点:如果用WCF在程序A中上传了一个很大的文件,那么程序B的执行要等待很长的时间。使得客户端程序的响应能力大大的下降。

优点:可以向客户端返回错误信息。

代码设置

[OperationContract]string ShowName(string name);

实例

//服务端接口using System.ServiceModel;namespace WCFService_Default{    [ServiceContract]    public interface IUser    {        [OperationContract]        string ShowName(string name);    }}//服务端实现namespace WCFService_Default{    public class User : IUser    {        public string ShowName(string name)        {            //线程睡眠20秒钟            System.Threading.Thread.Sleep(20000);            return "WCF服务,显示名称:" + name;        }    }}//客户端调用using System;using WCFClient_Default.WCFService_Default;namespace WCFClient_Default{    class Program    {        static void Main(string[] args)        {            UserClient client = new UserClient();            Console.WriteLine(DateTime.Now);            string result = client.ShowName("李林峰");            Console.WriteLine(result);            Console.WriteLine(DateTime.Now);            Console.ReadLine();        }    }}

单向模式

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

代码设置

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

优缺点与“请求响应模式”相反

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

实例

//服务端接口using System.ServiceModel;namespace WCFService_OneWay{    [ServiceContract]    public interface IUser    {        [OperationContract(IsOneWay = true)]        void DoSomething();    }}//服务端实现namespace WCFService_OneWay{    public class User : IUser    {        public void DoSomething()        {            //线程睡眠20秒钟            System.Threading.Thread.Sleep(20000);        }    }}//客户端调用using System;using WCFClient_OneWay.WCFService_OneWay;namespace WCFClient_OneWay{    class Program    {        static void Main(string[] args)        {            UserClient client = new UserClient();            Console.WriteLine(DateTime.Now);            //调用WCF服务的方法            client.DoSomething();            Console.WriteLine(DateTime.Now);            Console.ReadLine();        }    }}

双工模式

双工模式建立在上面两种模式的基础之上,实现客户端与服务端相互的调用

这里写图片描述

代码设置

[ServiceContract(CallbackContract = typeof(IUserCallback))]    public interface IUser    {        [OperationContract]        string ShowName(string name);    }   //回调的接口     public interface IUserCallback    {        [OperationContract(IsOneWay = true)]        void PrintSomething(string str);    }

支持回调的绑定有4种:WSDualHttpBinding、NetTcpBinding、NetNamedPipeBinding、NetPeerTcpBinding。

实例

配置文件中:

<endpoint address=""  binding="wsDualHttpBinding" contract="WCFService_DualPlex.IUser"></endpoint>

代码文件中:

//服务端接口using System.ServiceModel;namespace WCFService_DualPlex{    [ServiceContract(CallbackContract = typeof(IUserCallback))]    public interface IUser    {        [OperationContract]        string ShowName(string name);    }    public interface IUserCallback    {        [OperationContract(IsOneWay = true)]        void PrintSomething(string str);    }}//服务端实现using System.ServiceModel;namespace WCFService_DualPlex{    public class User : IUser    {        IUserCallback callback = null;        public User()        {            callback = OperationContext.Current.GetCallbackChannel<IUserCallback>();        }        public string ShowName(string name)        {            //在服务器端定义字符串,调用客户端的方法向客户端打印            string str = "服务器调用客户端...";            callback.PrintSomething(str);            //返回服务端方法            return "WCF服务,显示名称:" + name;        }    }}//客户端调用using System;using System.ServiceModel;using WCFClient_DualPlex.WCFService_DualPlex;namespace WCFClient_DualPlex{    //实现服务端的回调接口    public class CallbackHandler : IUserCallback    {        public void PrintSomething(string str)        {            Console.WriteLine(str);        }    }    class Program    {        static void Main(string[] args)        {            InstanceContext instanceContext = new InstanceContext(new CallbackHandler());            UserClient client = new UserClient(instanceContext);            Console.WriteLine(DateTime.Now);            string result = client.ShowName("Mark");            Console.WriteLine(result);            Console.WriteLine(DateTime.Now);            Console.ReadLine();        }    }}

参考

http://www.cnblogs.com/iamlilinfeng/archive/2012/10/03/2710698.html

原创粉丝点击