DeliveryRequirementsAttribute.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Dispatcher;
  9. using System.ServiceModel.Description;
  10. // This attribute specifies what the service implementation
  11. // requires from the binding that dispatches messages.
  12. [AttributeUsage(ServiceModelAttributeTargets.ContractBehavior, AllowMultiple = true)]
  13. public sealed class DeliveryRequirementsAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
  14. {
  15. Type contractType;
  16. QueuedDeliveryRequirementsMode queuedDeliveryRequirements = QueuedDeliveryRequirementsMode.Allowed;
  17. bool requireOrderedDelivery = false;
  18. // Used to implement IContractBehaviorAttribute; if null, DeliveryRequirementsAttribute applies to any contract
  19. public Type TargetContract
  20. {
  21. get { return contractType; }
  22. set { contractType = value; }
  23. }
  24. // RequireQueuedDelivery: Validates that any binding associated
  25. // with the service/channel supports Queued
  26. // delivery.
  27. //
  28. // DisallowQueuedDelivery: Validates that no binding associated
  29. // with the service/channel supports Queued
  30. // delivery.
  31. //
  32. // Ignore: Agnostic
  33. public QueuedDeliveryRequirementsMode QueuedDeliveryRequirements
  34. {
  35. get { return queuedDeliveryRequirements; }
  36. set
  37. {
  38. if (QueuedDeliveryRequirementsModeHelper.IsDefined(value))
  39. {
  40. queuedDeliveryRequirements = value;
  41. }
  42. else
  43. {
  44. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  45. }
  46. }
  47. }
  48. // True: Validates that any binding associated
  49. // with the service/channel supports Ordered
  50. // delivery.
  51. //
  52. // False: Does no validation.
  53. public bool RequireOrderedDelivery
  54. {
  55. get { return requireOrderedDelivery; }
  56. set { requireOrderedDelivery = value; }
  57. }
  58. void IContractBehavior.Validate(ContractDescription description, ServiceEndpoint endpoint)
  59. {
  60. if (description == null)
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  62. if (endpoint == null)
  63. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoint");
  64. ValidateEndpoint(endpoint);
  65. }
  66. void IContractBehavior.AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, BindingParameterCollection parameters)
  67. {
  68. }
  69. void IContractBehavior.ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime proxy)
  70. {
  71. }
  72. void IContractBehavior.ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch)
  73. {
  74. }
  75. void ValidateEndpoint(ServiceEndpoint endpoint)
  76. {
  77. string name = endpoint.Contract.ContractType.Name;
  78. EnsureQueuedDeliveryRequirements(name, endpoint.Binding);
  79. EnsureOrderedDeliveryRequirements(name, endpoint.Binding);
  80. }
  81. void EnsureQueuedDeliveryRequirements(string name, Binding binding)
  82. {
  83. if (QueuedDeliveryRequirements == QueuedDeliveryRequirementsMode.Required
  84. || QueuedDeliveryRequirements == QueuedDeliveryRequirementsMode.NotAllowed)
  85. {
  86. IBindingDeliveryCapabilities caps = binding.GetProperty<IBindingDeliveryCapabilities>(new BindingParameterCollection());
  87. if (caps == null)
  88. {
  89. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  90. SR.GetString(SR.SinceTheBindingForDoesnTSupportIBindingCapabilities2_1, name)));
  91. }
  92. else
  93. {
  94. bool queuedTransport = caps.QueuedDelivery;
  95. if (QueuedDeliveryRequirements == QueuedDeliveryRequirementsMode.Required && !queuedTransport)
  96. {
  97. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  98. SR.GetString(SR.BindingRequirementsAttributeRequiresQueuedDelivery1, name)));
  99. }
  100. else if (QueuedDeliveryRequirements == QueuedDeliveryRequirementsMode.NotAllowed && queuedTransport)
  101. {
  102. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  103. SR.GetString(SR.BindingRequirementsAttributeDisallowsQueuedDelivery1, name)));
  104. }
  105. }
  106. }
  107. }
  108. void EnsureOrderedDeliveryRequirements(string name, Binding binding)
  109. {
  110. if (RequireOrderedDelivery)
  111. {
  112. IBindingDeliveryCapabilities caps = binding.GetProperty<IBindingDeliveryCapabilities>(new BindingParameterCollection());
  113. if (caps == null)
  114. {
  115. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  116. SR.GetString(SR.SinceTheBindingForDoesnTSupportIBindingCapabilities1_1, name)));
  117. }
  118. else
  119. {
  120. if (!caps.AssuresOrderedDelivery)
  121. {
  122. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  123. SR.GetString(SR.TheBindingForDoesnTSupportOrderedDelivery1, name)));
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }