WCF Service Contract之MEPs
LazyBee
WCF支持的消息交换模式MEPs(Message Exchange Partten)为三种:请求-响应模式(Request-Replay), 单向模式(One Way),双向模式(Duplex).
[ServiceContract(SessionMode=SessionMode.Required)]
public interface ICalculatorSession
{
[OperationContract(IsOneWay = true)]
void AddTo(double n);
[OperationContract]
double Equals();
}
以上AddTo方法就是单向模式,而Equals就是双向模式。(缺省的IsOneWay的值是false)
缺省的消息交换模式就是Request-Replay模式。
注意:如果指定的方法的消息交换模式是One Way,那么这个方法的返回值必须是void,并且不能有out和ref的参数。当WCF服务的客户端不应该等到对应的操作的完成并且也不需要处理SOAP错误时,采用这种单向模式。(这个方法的返回值是void并不代表该方法会返回消息给调用者。)
Duplex模式是客户端和服务之间可以相互独立的使用One Way和Request-Replay进行双向通讯的消息交换模式。
在使用Duplex进行消息交换的时候,并且客户端调用的是定义为Request-Reply的服务操作,而且在这个服务操作中又调用客户端的另外一个Request-Reply的一个回调方法,这时就会遇到如下错误,你可以根据提示进行修复操作。
错误:This operation would deadlock because the reply cannot be received until the current Message completes processing. If you want to allow out-of-order message processing, specify ConcurrencyMode of Reentrant(可重入的并发模式) or Multiple on ServiceBehaviorAttribute.
//Service.svc的内容
===================================================================================
<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService" %>
1 首先在IIS中创建一个http://localhost/servicemodelsamples的虚拟目录。
2 如果你是第一次运行WCF Service的话,需要将.svc扩展名和aspnet_isapi.dll关联,具体操作如下:
1. 打开IIS管理器.
2. 右单击 Web Sites 并且选择属性 Properties.
3. On the Home Directory tab, click 配置Configuration.
4. In the list of application mappings, verify that the .svc file is mapped to the aspnet_isapi.dll. If the file has not been mapped:
a. 单击增加按钮.
b. 在 Add/Edit Application Extension Mapping 对话框中, 单击浏览按钮
c. 找到aspnet_isapi.dll并单击Open.
d. 制定 .svc 扩展名
e. 确保Check that file exists 选项没有被选中.
f. 单击确定, 然后再单击确定回到站点属性窗口.
5. 单击确定关闭站点属性窗口。
3然后就可以在IE窗口中测试时候正常运行了。(http://localhost/servicemodelsamples/service.svc).
由于VS2008自带的WCFTestClient.exe不能测试duplex模式的WCF Service,所以必须手动来产生客户端测试,为了方便应对服务契约的变更,可以在client 的项目属性的Build Events中的Pre-Build event command line的文本框中增加如下命令以帮助自动生成客户端的代理类:
Call "D:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
svcutil /language:cs /noconfig /out:$(ProjectDir)generatedClient.cs /n:*,Microsoft.ServiceModel.Samples http://localhost/servicemodelsamples/service.svc?wsdl
============================================================================================================
以下是客户端的配置文件:
This operation would deadlock because the reply cannot be received until the current Message completes processing. If you want to allow out-of-order message processing, specify ConcurrencyMode of Reentrant(可重入的并发模式) or Multiple on ServiceBehaviorAttribute.