NetMsmqBinding.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.ComponentModel;
  7. using System.Configuration;
  8. using System.Runtime;
  9. using System.ServiceModel.Channels;
  10. using System.Xml;
  11. using Config = System.ServiceModel.Configuration;
  12. public class NetMsmqBinding : MsmqBindingBase
  13. {
  14. // private BindingElements
  15. BinaryMessageEncodingBindingElement encoding;
  16. NetMsmqSecurity security;
  17. public NetMsmqBinding()
  18. {
  19. Initialize();
  20. this.security = new NetMsmqSecurity();
  21. }
  22. public NetMsmqBinding(string configurationName)
  23. {
  24. Initialize();
  25. this.security = new NetMsmqSecurity();
  26. ApplyConfiguration(configurationName);
  27. }
  28. public NetMsmqBinding(NetMsmqSecurityMode securityMode)
  29. {
  30. if (!NetMsmqSecurityModeHelper.IsDefined(securityMode))
  31. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("mode", (int)securityMode, typeof(NetMsmqSecurityMode)));
  32. Initialize();
  33. this.security = new NetMsmqSecurity(securityMode);
  34. }
  35. NetMsmqBinding(NetMsmqSecurity security)
  36. {
  37. Initialize();
  38. Fx.Assert(security != null, "Invalid (null) NetMsmqSecurity value");
  39. this.security = security;
  40. }
  41. [DefaultValue(MsmqDefaults.QueueTransferProtocol)]
  42. public QueueTransferProtocol QueueTransferProtocol
  43. {
  44. get
  45. {
  46. return (this.transport as MsmqTransportBindingElement).QueueTransferProtocol;
  47. }
  48. set
  49. {
  50. (this.transport as MsmqTransportBindingElement).QueueTransferProtocol = value;
  51. }
  52. }
  53. public XmlDictionaryReaderQuotas ReaderQuotas
  54. {
  55. get { return encoding.ReaderQuotas; }
  56. set
  57. {
  58. if (value == null)
  59. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  60. value.CopyTo(encoding.ReaderQuotas);
  61. }
  62. }
  63. public NetMsmqSecurity Security
  64. {
  65. get { return this.security; }
  66. set { this.security = value; }
  67. }
  68. public EnvelopeVersion EnvelopeVersion
  69. {
  70. get { return EnvelopeVersion.Soap12; }
  71. }
  72. [DefaultValue(TransportDefaults.MaxBufferPoolSize)]
  73. public long MaxBufferPoolSize
  74. {
  75. get { return transport.MaxBufferPoolSize; }
  76. set
  77. {
  78. transport.MaxBufferPoolSize = value;
  79. }
  80. }
  81. internal int MaxPoolSize
  82. {
  83. get
  84. {
  85. return (transport as MsmqTransportBindingElement).MaxPoolSize;
  86. }
  87. set
  88. {
  89. (transport as MsmqTransportBindingElement).MaxPoolSize = value;
  90. }
  91. }
  92. [DefaultValue(MsmqDefaults.UseActiveDirectory)]
  93. public bool UseActiveDirectory
  94. {
  95. get
  96. {
  97. return (transport as MsmqTransportBindingElement).UseActiveDirectory;
  98. }
  99. set
  100. {
  101. (transport as MsmqTransportBindingElement).UseActiveDirectory = value;
  102. }
  103. }
  104. [EditorBrowsable(EditorBrowsableState.Never)]
  105. public bool ShouldSerializeReaderQuotas()
  106. {
  107. if (this.ReaderQuotas.MaxArrayLength != EncoderDefaults.MaxArrayLength)
  108. {
  109. return true;
  110. }
  111. if (this.ReaderQuotas.MaxBytesPerRead != EncoderDefaults.MaxBytesPerRead)
  112. {
  113. return true;
  114. }
  115. if (this.ReaderQuotas.MaxDepth != EncoderDefaults.MaxDepth)
  116. {
  117. return true;
  118. }
  119. if (this.ReaderQuotas.MaxNameTableCharCount != EncoderDefaults.MaxNameTableCharCount)
  120. {
  121. return true;
  122. }
  123. if (this.ReaderQuotas.MaxStringContentLength != EncoderDefaults.MaxStringContentLength)
  124. {
  125. return true;
  126. }
  127. return false;
  128. }
  129. [EditorBrowsable(EditorBrowsableState.Never)]
  130. public bool ShouldSerializeSecurity()
  131. {
  132. if (this.security.Mode != NetMsmqSecurity.DefaultMode)
  133. {
  134. return true;
  135. }
  136. if (this.security.Transport.MsmqAuthenticationMode != MsmqDefaults.MsmqAuthenticationMode ||
  137. this.security.Transport.MsmqEncryptionAlgorithm != MsmqDefaults.MsmqEncryptionAlgorithm ||
  138. this.security.Transport.MsmqSecureHashAlgorithm != MsmqDefaults.MsmqSecureHashAlgorithm ||
  139. this.security.Transport.MsmqProtectionLevel != MsmqDefaults.MsmqProtectionLevel)
  140. {
  141. return true;
  142. }
  143. if (this.security.Message.AlgorithmSuite != MsmqDefaults.MessageSecurityAlgorithmSuite ||
  144. this.security.Message.ClientCredentialType != MsmqDefaults.DefaultClientCredentialType)
  145. {
  146. return true;
  147. }
  148. return false;
  149. }
  150. void Initialize()
  151. {
  152. transport = new MsmqTransportBindingElement();
  153. encoding = new BinaryMessageEncodingBindingElement();
  154. }
  155. void InitializeFrom(MsmqTransportBindingElement transport, BinaryMessageEncodingBindingElement encoding)
  156. {
  157. // only set properties that have standard binding manifestations: MaxPoolSize *is not* one of them
  158. this.CustomDeadLetterQueue = transport.CustomDeadLetterQueue;
  159. this.DeadLetterQueue = transport.DeadLetterQueue;
  160. this.Durable = transport.Durable;
  161. this.ExactlyOnce = transport.ExactlyOnce;
  162. this.MaxReceivedMessageSize = transport.MaxReceivedMessageSize;
  163. this.ReceiveRetryCount = transport.ReceiveRetryCount;
  164. this.MaxRetryCycles = transport.MaxRetryCycles;
  165. this.ReceiveErrorHandling = transport.ReceiveErrorHandling;
  166. this.RetryCycleDelay = transport.RetryCycleDelay;
  167. this.TimeToLive = transport.TimeToLive;
  168. this.UseSourceJournal = transport.UseSourceJournal;
  169. this.UseMsmqTracing = transport.UseMsmqTracing;
  170. this.ReceiveContextEnabled = transport.ReceiveContextEnabled;
  171. this.QueueTransferProtocol = transport.QueueTransferProtocol;
  172. this.MaxBufferPoolSize = transport.MaxBufferPoolSize;
  173. this.UseActiveDirectory = transport.UseActiveDirectory;
  174. this.ValidityDuration = transport.ValidityDuration;
  175. this.ReaderQuotas = encoding.ReaderQuotas;
  176. }
  177. // check that properties of the HttpTransportBindingElement and
  178. // MessageEncodingBindingElement not exposed as properties on NetMsmqBinding
  179. // match default values of the binding elements
  180. bool IsBindingElementsMatch(MsmqTransportBindingElement transport, MessageEncodingBindingElement encoding)
  181. {
  182. // we do not have to check the transport match here: they always match
  183. if (!this.GetTransport().IsMatch(transport))
  184. return false;
  185. if (!this.encoding.IsMatch(encoding))
  186. return false;
  187. return true;
  188. }
  189. void ApplyConfiguration(string configurationName)
  190. {
  191. Config.NetMsmqBindingCollectionElement section = Config.NetMsmqBindingCollectionElement.GetBindingCollectionElement();
  192. Config.NetMsmqBindingElement element = section.Bindings[configurationName];
  193. if (element == null)
  194. {
  195. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  196. SR.GetString(SR.ConfigInvalidBindingConfigurationName,
  197. configurationName,
  198. Config.ConfigurationStrings.NetMsmqBindingCollectionElementName)));
  199. }
  200. else
  201. {
  202. element.ApplyConfiguration(this);
  203. }
  204. }
  205. public override BindingElementCollection CreateBindingElements()
  206. { // return collection of BindingElements
  207. BindingElementCollection bindingElements = new BindingElementCollection();
  208. // order of BindingElements is important
  209. // add security
  210. SecurityBindingElement wsSecurity = CreateMessageSecurity();
  211. if (wsSecurity != null)
  212. {
  213. bindingElements.Add(wsSecurity);
  214. }
  215. // add encoding (text or mtom)
  216. bindingElements.Add(encoding);
  217. // add transport
  218. bindingElements.Add(GetTransport());
  219. return bindingElements.Clone();
  220. }
  221. internal static bool TryCreate(BindingElementCollection elements, out Binding binding)
  222. {
  223. binding = null;
  224. if (elements.Count > 3)
  225. return false;
  226. SecurityBindingElement security = null;
  227. BinaryMessageEncodingBindingElement encoding = null;
  228. MsmqTransportBindingElement transport = null;
  229. foreach (BindingElement element in elements)
  230. {
  231. if (element is SecurityBindingElement)
  232. security = element as SecurityBindingElement;
  233. else if (element is TransportBindingElement)
  234. transport = element as MsmqTransportBindingElement;
  235. else if (element is MessageEncodingBindingElement)
  236. encoding = element as BinaryMessageEncodingBindingElement;
  237. else
  238. return false;
  239. }
  240. UnifiedSecurityMode mode;
  241. if (!IsValidTransport(transport, out mode))
  242. return false;
  243. if (encoding == null)
  244. return false;
  245. NetMsmqSecurity netMsmqSecurity;
  246. if (!TryCreateSecurity(security, mode, out netMsmqSecurity))
  247. return false;
  248. NetMsmqBinding netMsmqBinding = new NetMsmqBinding(netMsmqSecurity);
  249. netMsmqBinding.InitializeFrom(transport, encoding);
  250. if (!netMsmqBinding.IsBindingElementsMatch(transport, encoding))
  251. return false;
  252. binding = netMsmqBinding;
  253. return true;
  254. }
  255. SecurityBindingElement CreateMessageSecurity()
  256. {
  257. if (this.security.Mode == NetMsmqSecurityMode.Message || this.security.Mode == NetMsmqSecurityMode.Both)
  258. {
  259. return this.security.CreateMessageSecurity();
  260. }
  261. else
  262. {
  263. return null;
  264. }
  265. }
  266. static bool TryCreateSecurity(SecurityBindingElement sbe, UnifiedSecurityMode mode, out NetMsmqSecurity security)
  267. {
  268. if (sbe != null)
  269. mode &= UnifiedSecurityMode.Message | UnifiedSecurityMode.Both;
  270. else
  271. mode &= ~(UnifiedSecurityMode.Message | UnifiedSecurityMode.Both);
  272. NetMsmqSecurityMode netMsmqSecurityMode = NetMsmqSecurityModeHelper.ToSecurityMode(mode);
  273. Fx.Assert(NetMsmqSecurityModeHelper.IsDefined(netMsmqSecurityMode), string.Format("Invalid NetMsmqSecurityMode value: {0}.", netMsmqSecurityMode.ToString()));
  274. if (NetMsmqSecurity.TryCreate(sbe, netMsmqSecurityMode, out security))
  275. {
  276. return true;
  277. }
  278. return false;
  279. }
  280. MsmqBindingElementBase GetTransport()
  281. {
  282. this.security.ConfigureTransportSecurity(transport);
  283. return transport;
  284. }
  285. static bool IsValidTransport(MsmqTransportBindingElement msmq, out UnifiedSecurityMode mode)
  286. {
  287. mode = (UnifiedSecurityMode)0;
  288. if (msmq == null)
  289. return false;
  290. return NetMsmqSecurity.IsConfiguredTransportSecurity(msmq, out mode);
  291. }
  292. }
  293. }