本文主要是介绍老老实实 学习WCF 第十天 wsDualHttpBinding 双工模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、服务器搭建
1、HelloWCFService.cs
using System;
using System.ServiceModel;namespace LearnWCF
{/**定义两个协定接口,一个 服务协定接口 IHelloWCF,一个 回调协定接口 IHelloWCFCallback,* 把 服务协定接口 的实现类写在 服务端 ,* 把 回调协定接口 的实现类写在 客户端 。 **///SessionMode = SessionMode.Required 允许会话//CallbackContract 回调协议[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IHelloWCFCallback))]public interface IHelloWCF{ //IsOneWay=true 单向通讯,不需要返回[OperationContract(IsOneWay=true)]void HelloWCF();}public interface IHelloWCFCallback{[OperationContract(IsOneWay=true)]void Callback(string msg);}public class HelloWCFService : IHelloWCF{private int _Counter;public void HelloWCF(){System.Threading.Thread.Sleep(5000);string msg = "Hello From Service! Time" + DateTime.Now.ToLongTimeString();//获得回调通道(通过操作上下文,获得当前操作的客户端实例通道)IHelloWCFCallback callbackChannel = OperationContext.Current.GetCallbackChannel<IHelloWCFCallback>();//调用回调操作callbackChannel.Callback(msg);}}}
2、HelloWCFService.svc
<%@ServiceHost language=c# Debug="true" Service="LearnWCF.HelloWCFService"%>
3、Web.Config
<configuration><system.serviceModel><services><service name="LearnWCF.HelloWCFService" behaviorConfiguration="metadataExchange"><endpoint address="" binding="wsDualHttpBinding" contract="LearnWCF.IHelloWCF"/><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/></service></services><behaviors><serviceBehaviors><behavior name="metadataExchange"><serviceMetadata httpGetEnabled="true" /></behavior></serviceBehaviors></behaviors></system.serviceModel>
</configuration>
二、客户端
这篇关于老老实实 学习WCF 第十天 wsDualHttpBinding 双工模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!