对WCF的可靠会话编程有一定了解的人应该知道,我们可以使用 DeliveryRequirementsAttribute 可以指示WCF确认绑定提供服务或客户端实现所需的功能。如果在从应用程序配置文件加载服务说明或在代码中以编程方式生成服务说明时检测到 DeliveryRequirementsAttribute 属性,则 WCF 会验证所配置的绑定,并支持该属性指定的所有功能。例如,您的服务可能要求绑定支持队列。使用 DeliveryRequirementsAttribute 可以让WCF 确认是否满足下列要求:
-
QueuedDeliveryRequirements 属性指定绑定必须满足的队列要求。
-
RequireOrderedDelivery 属性指示绑定是否必须支持有序消息传递。
-
TargetContract 属性指示要求所适用的类型。
但是,当你使用DeliveryRequirementsAttribute 特性的时候,会出现一些很奇怪的现象。经过我个人的分析,这是WCF的一个Bug。
一、问题再现
我随便定义了一个简单的服务:OrderService。并在服务契约上应用了一个DeliveryRequirementsAttribute 特性,将RequireOrderedDelivery和QueuedDeliveryRequirements分别设置成TRUE和QueuedDeliveryRequirementsMode.Allowed,也就是允许终结点的绑定采用队列传递,但是要求绑定采用有序消息交付。
1: [Serializable]
2: public class Order
3: { }
4: [ServiceContract]
5: [DeliveryRequirements(RequireOrderedDelivery = true, QueuedDeliveryRequirements = QueuedDeliveryRequirementsMode.Allowed )]
6: public interface IOrderService
7: {
8: [OperationContract]
9: void ProcessOrder(Order order);
10: }
11: public class OrderService : IOrderService
12: {
13: public void ProcessOrder(Order order)
14: {
15: throw new NotImplementedException();
16: }
17: }
现在,我通过下面的代码对服务进行寄宿,注意终结点绑定的可靠会话特性被开启,但是Ordered属性被设置成False。也就是该绑定不满足通过DeliveryRequirementsAttribute 设置的对有序消息交付的要求。
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: using (ServiceHost host = new ServiceHost(typeof(OrderService)))
6: {
7: WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message, true);
8: binding.ReliableSession.Ordered = false;
9: host.AddServiceEndpoint(typeof(IOrderService), binding, "http://127.0.0.1:3721/orderservice");
10: host.Open();
11: Console.Read();
12: }
13: }
14: }
当上面的代码执行到ServiceHost开启(host.Open();),会抛出如下的异常。异常信息是“The DeliveryRequirementsAttribute on contract 'IOrderService' specifies a QueuedDeliveryRequirements value of NotAllowed. However, the configured binding for this contract specifies that it does support queued delivery. A queued binding may not be used with this contract.”翻译成中文就是“服务契约'IOrderService' 上的 DeliveryRequirementsAttribute 指定了 NotAllowed 的 QueuedDeliveryRequirements 值。但是,为此协定配置的绑定指定其不支持排队传送。排队绑定可能不能用于此契约”。实际上在这里QueuedDeliveryRequirements是Allowed,不应该出现如此的错误信息。
二、资源的错误定义导致异常消息不正确
我们对上面抛出的异常进行进一步地追踪,你会现在该异常的StackTrace如下。可以看出来,异常是在执行DeliveryRequirementsAttribute 的EnsureOrderedDeliveryRequirements方法时抛出来的。
at System.ServiceModel.DeliveryRequirementsAttribute.EnsureOrderedDeliveryRequirements(String name, Binding binding)
at System.ServiceModel.DeliveryRequirementsAttribute.ValidateEndpoint(ServiceEndpoint endpoint)
at System.ServiceModel.DeliveryRequirementsAttribute.System.ServiceModel.Description.IContractBehavior.Validate(ContractDescription description, ServiceEndpoint endpoint)
at System.ServiceModel.Description.ServiceEndpoint.Validate(Boolean runOperationValidators, Boolean isForService)
at System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.ServiceHostBase.InitializeRuntime()
at System.ServiceModel.ServiceHostBase.OnBeginOpen()
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
at ConsoleApplication4.Program.Main(String[] args) in D:/Users/jinnan/Documents/Visual Studio 2008/Projects/ConsoleApplication4/ConsoleApplication4/Program.cs:line 23
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
实际上,从名称就可能看出EnsureOrderedDeliveryRequirements方法是在验证终结点绑定的有序消息交付能,和队列消息传递一点关系都没有。通过Reflector,我们看看EnsureOrderedDeliveryRequirements方法的实现。
1: private void EnsureOrderedDeliveryRequirements(string name, Binding binding)
2: {
3: if (this.RequireOrderedDelivery)
4: {
5: IBindingDeliveryCapabilities property = binding.GetProperty<IBindingDeliveryCapabilities>(new BindingParameterCollection());
6: if (property == null)
7: {
8: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString("SinceTheBindingForDoesnTSupportIBindingCapabilities1_1", new object[] { name })));
9: }
10: if (!property.AssuresOrderedDelivery)
11: {
12: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString("TheBindingForDoesnTSupportOrderedDelivery1", new object[] { name })));
13: }
14: }
15: }
16:
17:
18:
19:
从上面的逻辑我们可以看到,如果“有序消息交付”验证失败会抛出InvalidOperationException异常,这和我们前面的StackTrace是一致的。而异常消息则定义在资源文件中。该资源文件的Key是“TheBindingForDoesnTSupportOrderedDelivery1”。为此,在此利用Reflector,看看资源项的定义,结果证实资源字符串的内容和上面抛出的异常消息是吻合的。所以,我们可以说由于WCF资源字符串的错误定义或者错误使用导致了这个Bug的产生。