本文主要是讲关于oneway、duplex以及正常这三种情况在WCF中如何实现。
废话不提,开始。
1.定义契约( Area.WCF.Contract)(注意System.ServerModel的特性)
1).oneway模式:
/// <summary>
/// •One-Way Calls
/// 在这种交换模式中,存在着如下的特征
///•没有返回值,返回类型只能为void
///•不能包含ref或者out类型的参数
///•只有客户端发起请求,服务端并不会对请求进行回复。
/// 通过设置OperationContract的IsOneWay=True可以将满足要求的方法设置为这种消息交换模式
/// </summary>
[ServiceContract]
public interface IOneWayJob
{
/// <summary>
/// 单工执行任务
/// </summary>
/// <param name="Name"></param>
[OperationContract(IsOneWay=true)]
void Do(string Name);
}
2). 正常模式
/// <summary>
/// Request/Reply
/// 这种交换模式是使用最多的一中,它有如下特征:
///•调用服务方法后需要等待服务的消息返回,即便该方法返回 void 类型
///•相比Duplex来讲,这种模式强调的是客户端的被动接受,也就是说客户端接受到响应后,消息交换就结束了。
///•在这种模式下,服务端永远是服务端,客户端就是客户端,职责分明。
///它是缺省的消息交换模式,设置OperationContract便可以设置为此种消息交换模式
[ServiceContract]
public interface INormalJob
{
/// <summary>
/// 客户端请求方式执行任务
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
[OperationContract]
string Do(string Name);
}
3).Duplex模式
/// <summary>
/// Duplex
/// 因为它可以在处理完请求之后,通过请求客户端中的回调进行响应操作
/// 1.消息交换过程中,服务端和客户端角色会发生调换
/// 2.服务端处理完请求后,返回给客户端的不是reply,而是callback请求。
/// Duplex模式对Bindding有特殊的要求,它要求支持Duplex MEP(Message Exchange Pattern),如WSDualHttpBinding和NetTcpBinding
/// </summary>
[ServiceContract(CallbackContract = typeof(ICallback))]
public interface IJob
{
/// <summary>
/// 通过双工的通道来执行任务
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
[OperationContract]
string Do(string Name);
当然我们这里要加入ICALLBACK的接口
/// <summary>
/// 返回双通道执行完成花的时间
/// </summary>
/// <param name="UserdTime"></param>
[OperationContract]
void Done(int UserdTime);
2.实现契约(Area.WCF.Services)
即在该层引入此层DLL并实现对应的类即可,不赘述。
3.宿主承载(Area.WCF.Host)
主要是配置服务器客配置文件(主要是A、B、C原则)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Area.WCF.Services.Job">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8011/Services"/>
</baseAddresses>
</host>
<endpoint address="Duplex" binding="netTcpBinding" contract="Area.WCF.Contract.IJob">
<identity>
<dns value="locolhost"/>
</identity>
</endpoint>
</service>
<service name="Area.WCF.Services.NormalJob">
<host>
<baseAddresses>
<addbaseAddress="net.tcp://localhost:8011/Services"/>
</baseAddresses>
</host>
<endpoint address="Normal" binding="netTcpBinding" contract="Area.WCF.Contract.INormalJob">
<identity>
<dns value="locolhost"/>
</identity>
</endpoint>
</service>
<service name="Area.WCF.Services.OneWayJob">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8011/Services"/>
</baseAddresses>
</host>
<endpoint address="OneWay" binding="netTcpBinding" contract="Area.WCF.Contract.IOneWayJob">
<identity>
<dns value="locolhost"/>
</identity>
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
相关说明可以查MSDN或者百度,GOOGLE对应配置节意思。
4).客户端(Area.WCF.Client)
注意System.ServiceModel下的ClientBase类(用于创建可调用服务的WCF客户端对象)。(同时客户端去引用的方式有多种,此处讲手动生动对应的引用)
写下一个JOBCLIENT的客户端实现。
public partial class JobClient:ClientBase<IJob>,IJob
{
public JobClient()
{
}
public JobClient(string endpointsConfiguration) : base(endpointsConfiguration) { }
public JobClient(InstanceContext callbackInstance) : base(callbackInstance) { }
public JobClient(InstanceContext callbackInstance, string endpointCofigurationName) : base(callbackInstance, endpointCofigurationName) { }
public JobClient(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) : base(callbackInstance, endpointConfigurationName, remoteAddress) { }
public JobClient(InstanceContext callbackInstance, Binding binding, EndpointAddress endpointAddress) : base(callbackInstance, binding, endpointAddress) { }
public JobClient(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress endpointAddress) : base(callbackInstance, endpointConfigurationName, endpointAddress) { }
#region IJob 成员
public string Do(string Name)
{
return base.Channel.Do(Name);
}
#endregion
}
说明,base.Channel.Do(Name)中,base即为:当前可创建的服务的WCF客户端对象,Channel:获取与报务器通信的内部信道
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com