双工消息交换模式 Duplex MEP
双工是指同时发送和接收消息。双工消息交换模式定义了一个允许接收者和发送者同时发送消息的操作集合,反之亦然。
双工消息交换模式与WSDL
与双工消息交换模式相关的WSDL包含两个操作:一个操作(SomeOperation)表示消息发送给接收者,另外一个操作(SomeCallbackOperation)表示消息从接收者回发给发送者。
双工消息交换模式与WCF契约
双工通信需要两个契约。习惯上,描述发送给接收应用程序的消息(和应答消息,如果有)的契约称为服务契约,描述接收者发送给发送者的契约称为回调契约。这两个契约通过服务契约的ServiceContractAttribute.CallbackContract属性关联。
[csharp]
//Service契约
using System.ServiceModel;
using CallbackContract;
namespace WcfService4
{
[ServiceContract(
Name = "IServiceContract",
CallbackContract = typeof(ICallbackService),
Namespace = "http://www.andersoft.com/1ServiceContract")]
public inte易做图ce IServiceContract
{
[OperationContract(
Name="Sum",
IsOneWay = true)]
void Sum(int value1, int value2);
}
}
//Service契约
using System.ServiceModel;
using CallbackContract;
namespace WcfService4
{
[ServiceContract(
Name = "IServiceContract",
CallbackContract = typeof(ICallbackService),
Namespace = "http://www.andersoft.com/1ServiceContract")]
public inte易做图ce IServiceContract
{
[OperationContract(
Name="Sum",
IsOneWay = true)]
void Sum(int value1, int value2);
}
}
[csharp]
//Service 实现
using System.ServiceModel;
using CallbackContract;
namespace WcfService4
{
public class ServiceAPIs : IServiceContract
{
public void Sum(int value1, int value2)
{
int res = value1 + value2;
ICallbackService callback = OperationContext.Current.GetCallbackChannel<ICallbackService>();
callback.SumCallback(value1, value2, res);
}
}
}
//Service 实现
using System.ServiceModel;
using CallbackContract;
namespace WcfService4
{
public class ServiceAPIs : IServiceContract
{
public void Sum(int value1, int value2)
{
int res = value1 + value2;
ICallbackService callback = OperationContext.Current.GetCallbackChannel<ICallbackService>();
callback.SumCallback(value1, value2, res);
}
}
}
[csharp]
//Callback 接口
using System.ServiceModel;
namespace CallbackContract
{
public inte易做图ce ICallbackService
{
[OperationContract(
Name="SumCallback",
IsOneWay=true)]
void SumCallback(int value1, int value2, int res);
}
}
//Callback 接口
using System.ServiceModel;
namespace CallbackContract
{
public inte易做图ce ICallbackService
{
[OperationContract(
Name="SumCallback",
IsOneWay=true)]
void SumCallback(int value1, int value2, int res);
}
}
[csharp]
//Callback API
using System;
namespace CallbackContract
{
public class CallbackAPIs : ICallbackService
{
public void SumCallback(int value1, int value2, int res)
{
Console.WriteLine("{0} + {1} = {2}", value1, value2, res);
}
}
}
//Callback API
using System;
namespace CallbackContract
{
public class CallbackAPIs : ICallbackService
{
public void SumCallback(int value1, int value2, int res)
{
Console.WriteLine("{0} + {1} = {2}", value1, value2, res);
}
}
}
[csharp]
//Host
using System;
using System.ServiceModel;
using WcfService4;
namespace SVC
{
class Program
{
static void Main(string[] args)
{
NetTcpBinding binding = new NetTcpBinding();
ServiceHost svc = new ServiceHost(typeof(ServiceAP
补充:Web开发 , ASP.Net ,