1.请求应答模式:在WCF接口中使用默认[OperationContract] //不指定参数时默认使用 请求应答模式,客户端调用服务,客户端请求才能应答
2.单向模式(IsOneWay):在WCF接口中使用特性 [OperationContract(IsOneWay = true)],仅服务端接收信息
3.双工模式: 在客户端 服务端各自实现一个接口 在客户端的接口称之为回调接口函数 服务端主动调用向客户端发送信息
使用接口特性**[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IServiceDuplexcallback))]**
public interface Iservice{} //用于服务
public interface IServiceDuplexcallback{} //用于回调
服务端: 在服务类中使用以下代码获得客户端回调函数实体对象
OperationContext context = OperationContext.Current;
IServiceDuplexcallback callback = context.GetCallbackChannel<IServiceDuplexcallback>();
客户端:在客户端类中引用,继承接口使用以下代码,创建client客户端对象
//InstanceContext类: 用于 传递 客户端的实现对象 给 服务端.
//(让服务端知道通信对象是谁)
InstanceContext context = new InstanceContext(new CallbackHandler());
//CallbackHandler 是实现接口的类
ServiceClient client = new ServiceClient(context,"服务端终结点名称");
//第二个参数可有可无,与Endpoint前的Service属性name匹配,指定某个服务端通信
三种模式相应的在Web.config中修改配置或加入
1.<service name="WcfService1.Service1">
<endpoint binding="wsHttpBinding" contract="WcfService1.IService1">
</endpoint>
</service>
2. <service name="WcfService1.Service2">
<endpoint binding="wsDualHttpBinding" contract="WcfService1.IService2"></endpoint>
</service>
3. <service name="WcfService1.Service3">
<endpoint binding="wsDualHttpBinding" contract="WcfService1.IService3"></endpoint>
</service>
</services>