WCF通信模式

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值