PeerCustomResolverBindingElement.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.ComponentModel;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Configuration;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Dispatcher;
  11. using System.ServiceModel.PeerResolvers;
  12. [ObsoleteAttribute ("PeerChannel feature is obsolete and will be removed in the future.", false)]
  13. public sealed class PeerCustomResolverBindingElement : PeerResolverBindingElement
  14. {
  15. EndpointAddress address;
  16. Binding binding;
  17. string bindingSection, bindingConfiguration;
  18. //this should be PeerCustomResolver?
  19. PeerResolver resolver;
  20. ClientCredentials credentials;
  21. PeerReferralPolicy referralPolicy;
  22. public PeerCustomResolverBindingElement() { }
  23. public PeerCustomResolverBindingElement(PeerCustomResolverBindingElement other)
  24. : base(other)
  25. {
  26. this.address = other.address;
  27. this.bindingConfiguration = other.bindingConfiguration;
  28. this.bindingSection = other.bindingSection;
  29. this.binding = other.binding;
  30. this.resolver = other.resolver;
  31. this.credentials = other.credentials;
  32. }
  33. public PeerCustomResolverBindingElement(PeerCustomResolverSettings settings)
  34. {
  35. if (settings != null)
  36. {
  37. this.address = settings.Address;
  38. this.binding = settings.Binding;
  39. this.resolver = settings.Resolver;
  40. this.bindingConfiguration = settings.BindingConfiguration;
  41. this.bindingSection = settings.BindingSection;
  42. }
  43. }
  44. public PeerCustomResolverBindingElement(BindingContext context, PeerCustomResolverSettings settings)
  45. : this(settings)
  46. {
  47. if (context == null)
  48. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("context"));
  49. #pragma warning suppress 56506 // [....], context.BindingParameters is never null
  50. credentials = context.BindingParameters.Find<ClientCredentials>();
  51. }
  52. public override T GetProperty<T>(System.ServiceModel.Channels.BindingContext context)
  53. {
  54. #pragma warning suppress 56506 // context could be null. Pre-4.0 behaviour, won't fix in Dev10.
  55. return context.GetInnerProperty<T>();
  56. }
  57. public EndpointAddress Address
  58. {
  59. get
  60. {
  61. return address;
  62. }
  63. set
  64. {
  65. address = value;
  66. }
  67. }
  68. public Binding Binding
  69. {
  70. get
  71. {
  72. return binding;
  73. }
  74. set
  75. {
  76. binding = value;
  77. }
  78. }
  79. public override PeerReferralPolicy ReferralPolicy
  80. {
  81. get
  82. {
  83. return referralPolicy;
  84. }
  85. set
  86. {
  87. if (!PeerReferralPolicyHelper.IsDefined(value))
  88. {
  89. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("value", (int)value, typeof(PeerReferralPolicy)));
  90. }
  91. referralPolicy = value;
  92. }
  93. }
  94. public override BindingElement Clone()
  95. {
  96. return new PeerCustomResolverBindingElement(this);
  97. }
  98. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
  99. {
  100. if (context == null)
  101. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("context"));
  102. #pragma warning suppress 56506 // [....], context.BindingParameters is never null
  103. context.BindingParameters.Add(this);
  104. credentials = context.BindingParameters.Find<ClientCredentials>();
  105. return context.BuildInnerChannelFactory<TChannel>();
  106. }
  107. public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
  108. {
  109. if (context == null)
  110. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("context"));
  111. #pragma warning suppress 56506 // [....], context.BindingParameters is never null
  112. this.credentials = context.BindingParameters.Find<ClientCredentials>();
  113. context.BindingParameters.Add(this);
  114. return context.CanBuildInnerChannelFactory<TChannel>();
  115. }
  116. public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
  117. {
  118. if (context == null)
  119. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("context"));
  120. #pragma warning suppress 56506 // [....], context.BindingParameters is never null
  121. context.BindingParameters.Add(this);
  122. this.credentials = context.BindingParameters.Find<ClientCredentials>();
  123. return context.BuildInnerChannelListener<TChannel>();
  124. }
  125. public override bool CanBuildChannelListener<TChannel>(BindingContext context)
  126. {
  127. if (context == null)
  128. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("context"));
  129. #pragma warning suppress 56506 // [....], context.BindingParameters is never null
  130. this.credentials = context.BindingParameters.Find<ClientCredentials>();
  131. context.BindingParameters.Add(this);
  132. return context.CanBuildInnerChannelListener<TChannel>();
  133. }
  134. public override PeerResolver CreatePeerResolver()
  135. {
  136. if (resolver == null)
  137. {
  138. if (address == null || ((binding == null) && (String.IsNullOrEmpty(this.bindingSection) || String.IsNullOrEmpty(this.bindingConfiguration))))
  139. PeerExceptionHelper.ThrowArgument_InsufficientResolverSettings();
  140. if (binding == null)
  141. {
  142. this.binding = ConfigLoader.LookupBinding(this.bindingSection, this.bindingConfiguration);
  143. if (binding == null)
  144. PeerExceptionHelper.ThrowArgument_InsufficientResolverSettings();
  145. }
  146. resolver = new PeerDefaultCustomResolverClient();
  147. }
  148. if (resolver != null)
  149. {
  150. resolver.Initialize(address, binding, credentials, this.referralPolicy);
  151. if (resolver is PeerDefaultCustomResolverClient)
  152. {
  153. (resolver as PeerDefaultCustomResolverClient).BindingName = this.bindingSection;
  154. (resolver as PeerDefaultCustomResolverClient).BindingConfigurationName = this.bindingConfiguration;
  155. }
  156. }
  157. return resolver;
  158. }
  159. }
  160. }