MsmqIntegrationValidationBehavior.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.MsmqIntegration
  5. {
  6. using System.ServiceModel;
  7. using System.ServiceModel.Dispatcher;
  8. using System.ServiceModel.Description;
  9. using System.ServiceModel.Channels;
  10. using System.Collections.ObjectModel;
  11. using System.Collections.Generic;
  12. class MsmqIntegrationValidationBehavior : IEndpointBehavior, IServiceBehavior
  13. {
  14. static MsmqIntegrationValidationBehavior instance;
  15. internal static MsmqIntegrationValidationBehavior Instance
  16. {
  17. get
  18. {
  19. if (instance == null)
  20. instance = new MsmqIntegrationValidationBehavior();
  21. return instance;
  22. }
  23. }
  24. MsmqIntegrationValidationBehavior() { }
  25. void IEndpointBehavior.Validate(ServiceEndpoint serviceEndpoint)
  26. {
  27. if (serviceEndpoint == null)
  28. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceEndpoint");
  29. ContractDescription contract = serviceEndpoint.Contract;
  30. Binding binding = serviceEndpoint.Binding;
  31. if (NeedValidateBinding(binding))
  32. {
  33. ValidateHelper(contract, binding, null);
  34. }
  35. }
  36. void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters)
  37. {
  38. }
  39. void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
  40. {
  41. }
  42. void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
  43. {
  44. }
  45. void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  46. {
  47. }
  48. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
  49. {
  50. }
  51. void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  52. {
  53. if (description == null)
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  55. for (int i = 0; i < description.Endpoints.Count; i++)
  56. {
  57. ServiceEndpoint endpoint = description.Endpoints[i];
  58. if (NeedValidateBinding(endpoint.Binding))
  59. {
  60. ValidateHelper(endpoint.Contract, endpoint.Binding, description);
  61. break;
  62. }
  63. }
  64. }
  65. bool NeedValidateBinding(Binding binding)
  66. {
  67. if (binding is MsmqIntegrationBinding)
  68. return true;
  69. if (binding is CustomBinding)
  70. {
  71. CustomBinding customBinding = new CustomBinding(binding);
  72. return (customBinding.Elements.Find<MsmqIntegrationBindingElement>() != null);
  73. }
  74. return false;
  75. }
  76. void ValidateHelper(ContractDescription contract, Binding binding, ServiceDescription description)
  77. {
  78. foreach (OperationDescription operation in contract.Operations)
  79. {
  80. // since this is one-way, we can only have one message (one-way requirement is validated elsewhere)
  81. MessageDescription message = operation.Messages[0];
  82. if ((message.Body.Parts.Count == 0) && (message.Headers.Count == 0))
  83. // all message parts are properties, great
  84. continue;
  85. if (message.Body.Parts.Count == 1) // Single MsmqMessage<> argument is also legal
  86. {
  87. Type type = message.Body.Parts[0].Type;
  88. if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(MsmqMessage<>)))
  89. continue;
  90. }
  91. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  92. SR.GetString(SR.MsmqInvalidServiceOperationForMsmqIntegrationBinding, binding.Name, operation.Name, contract.Name)));
  93. }
  94. }
  95. }
  96. }