在WCF通信中,有三种消息交换模式,OneWay(单向模式), Request/Reponse(请求回复模式), Duplex(双工通信模式)这三种通信方式。下面对这三种消息交换模式进行讲解。
1. OneWay模式,
[ServiceContract]public interface IOneWayService{[OperationContract(IsOneWay = true)]void Add(double x, double y);}
需要在OperationContract特性中显示的添加IsOneWay=true来标识这个操作是OneWay的,另外需要注意,此时的方法是不可以带返回值的。如果包含返回值,在启动Service后,可以看到下面截图所示的错误信息,
2. Request/Reponse模式,
当我们定义一个服务的操作时,该操作默认是请求回复模式的。不管该方法是否有返回值。可以通过Fiddler进行监控。
[ServiceContract]public interface IRequestReponseService{[OperationContract]void Add(double x, double y);}
通过Fidder发现,当没有返回值时,其实服务给了一个默认的回复消息,
3. Duplex模式
定义双工通信:
[ServiceContract(CallbackContract = typeof(IDuplexCallbackService))]public interface IDuplexService{[OperationContract(IsOneWay = true)]void GetDuplexServiceResult(string username, string password);}
public interface IDuplexCallbackService{[OperationContract(IsOneWay = true)]void ShowDuplexServiceResult(IEnumerable<Department> depts);}
在定义服务契约时,同时指定回掉契约。
实现IDuplexService:
public class DuplexService : IDuplexService{public void GetDuplexServiceResult(string username, string password){if (string.IsNullOrWhiteSpace(username) ||string.IsNullOrWhiteSpace(password)){return;}if(username == "Yang-Fei" && password == "123456"){List<Department> depts = new List<Department>(){new Department() { Id = 100, Name="Development" },new Department() { Id = 101, Name="Sales" },new Department() { Id = 102, Name="Operation" },};IDuplexCallbackService callback =OperationContext.Current.GetCallbackChannel<IDuplexCallbackService>();callback.ShowDuplexServiceResult(depts);}}}
我们可以使用netTcpBinding/wsDualHttpBinding来作为双工通信的binding。
App.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /></startup><system.serviceModel><services><service name="Services.DuplexService" behaviorConfiguration="DuplexServiceBehavior"><endpoint address="" binding="netTcpBinding" contract="Services.IDuplexService"><identity><dns value="localhost"/></identity></endpoint><endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange"/><host><baseAddresses><add baseAddress="net.tcp://10.12.65.145/DuplexService"/></baseAddresses></host></service></services><behaviors><serviceBehaviors><behavior name="DuplexServiceBehavior"><serviceMetadata httpGetEnabled="false"/><serviceDebug includeExceptionDetailInFaults="true"/></behavior></serviceBehaviors></behaviors></system.serviceModel> </configuration>
需要在客户端实现回掉服务,例如:
public class DuplexCallbackServiceClient : IDuplexServiceCallback{public void ShowDuplexServiceResult(Department[] depts){foreach (Department dept in depts){Console.WriteLine("Dept Id: {0} Dept Name: {1}", dept.Id, dept.Name);}}}
对服务进行调用:
static void Main(string[] args){DuplexCallbackServiceClient client = new DuplexCallbackServiceClient();InstanceContext context = new InstanceContext(client);using (DuplexServiceClient proxy = new DuplexServiceClient(context)){string username = "Yang-Fei";string password = "123456";proxy.GetDuplexServiceResult(username, password);}Console.ReadKey();}
运行结果:
代码点击这里下载,
感谢您的阅读。