MsmqIntegrationOutputChannel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.MsmqIntegration
  5. {
  6. using System.IO;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Diagnostics;
  11. using System.ServiceModel.Security.Tokens;
  12. sealed class MsmqIntegrationOutputChannel : TransportOutputChannel
  13. {
  14. MsmqQueue msmqQueue;
  15. MsmqTransactionMode transactionMode;
  16. MsmqIntegrationChannelFactory factory;
  17. SecurityTokenProviderContainer certificateTokenProvider;
  18. public MsmqIntegrationOutputChannel(MsmqIntegrationChannelFactory factory, EndpointAddress to, Uri via, bool manualAddressing)
  19. : base(factory, to, via, manualAddressing, factory.MessageVersion)
  20. {
  21. this.factory = factory;
  22. if (factory.IsMsmqX509SecurityConfigured)
  23. {
  24. this.certificateTokenProvider = factory.CreateX509TokenProvider(to, via);
  25. }
  26. }
  27. void CloseQueue()
  28. {
  29. if (null != this.msmqQueue)
  30. this.msmqQueue.Dispose();
  31. this.msmqQueue = null;
  32. }
  33. void OnCloseCore(bool isAborting, TimeSpan timeout)
  34. {
  35. this.CloseQueue();
  36. if (this.certificateTokenProvider != null)
  37. {
  38. if (isAborting)
  39. this.certificateTokenProvider.Abort();
  40. else
  41. this.certificateTokenProvider.Close(timeout);
  42. }
  43. }
  44. protected override void OnAbort()
  45. {
  46. this.OnCloseCore(true, TimeSpan.Zero);
  47. }
  48. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  49. {
  50. this.OnCloseCore(false, timeout);
  51. return new CompletedAsyncResult(callback, state);
  52. }
  53. protected override void OnEndClose(IAsyncResult result)
  54. {
  55. CompletedAsyncResult.End(result);
  56. }
  57. protected override void OnClose(TimeSpan timeout)
  58. {
  59. this.OnCloseCore(false, timeout);
  60. }
  61. void OpenQueue()
  62. {
  63. try
  64. {
  65. this.msmqQueue = new MsmqQueue(this.factory.AddressTranslator.UriToFormatName(this.RemoteAddress.Uri), UnsafeNativeMethods.MQ_SEND_ACCESS);
  66. }
  67. catch (MsmqException ex)
  68. {
  69. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
  70. }
  71. if (this.factory.ExactlyOnce)
  72. {
  73. this.transactionMode = MsmqTransactionMode.CurrentOrSingle;
  74. }
  75. else
  76. {
  77. this.transactionMode = MsmqTransactionMode.None;
  78. }
  79. }
  80. void OnOpenCore(TimeSpan timeout)
  81. {
  82. OpenQueue();
  83. if (this.certificateTokenProvider != null)
  84. {
  85. this.certificateTokenProvider.Open(timeout);
  86. }
  87. }
  88. protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  89. {
  90. OnOpenCore(timeout);
  91. return new CompletedAsyncResult(callback, state);
  92. }
  93. protected override void OnEndOpen(IAsyncResult result)
  94. {
  95. CompletedAsyncResult.End(result);
  96. }
  97. protected override void OnOpen(TimeSpan timeout)
  98. {
  99. OnOpenCore(timeout);
  100. }
  101. protected override IAsyncResult OnBeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state)
  102. {
  103. OnSend(message, timeout);
  104. return new CompletedAsyncResult(callback, state);
  105. }
  106. protected override void OnEndSend(IAsyncResult result)
  107. {
  108. CompletedAsyncResult.End(result);
  109. }
  110. protected override void OnSend(Message message, TimeSpan timeout)
  111. {
  112. MessageProperties properties = message.Properties;
  113. Stream stream = null;
  114. MsmqIntegrationMessageProperty property = MsmqIntegrationMessageProperty.Get(message);
  115. if (null == property)
  116. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.GetString(SR.MsmqMessageDoesntHaveIntegrationProperty)));
  117. if (null != property.Body)
  118. stream = this.factory.Serialize(property);
  119. int size;
  120. if (stream == null)
  121. {
  122. size = 0;
  123. }
  124. else
  125. {
  126. if (stream.Length > int.MaxValue)
  127. {
  128. throw TraceUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.MessageSizeMustBeInIntegerRange)), message);
  129. }
  130. size = (int)stream.Length;
  131. }
  132. using (MsmqIntegrationOutputMessage msmqMessage = new MsmqIntegrationOutputMessage(this.factory, size, this.RemoteAddress, property))
  133. {
  134. msmqMessage.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
  135. if (stream != null)
  136. {
  137. stream.Position = 0;
  138. for (int bytesRemaining = size; bytesRemaining > 0; )
  139. {
  140. int bytesRead = stream.Read(msmqMessage.Body.Buffer, 0, bytesRemaining);
  141. bytesRemaining -= bytesRead;
  142. }
  143. }
  144. bool lockHeld = false;
  145. try
  146. {
  147. Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
  148. this.msmqQueue.Send(msmqMessage, this.transactionMode);
  149. MsmqDiagnostics.DatagramSent(msmqMessage.MessageId, message);
  150. property.Id = MsmqMessageId.ToString(msmqMessage.MessageId.Buffer);
  151. }
  152. catch (MsmqException ex)
  153. {
  154. if (ex.FaultSender)
  155. this.Fault();
  156. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
  157. }
  158. finally
  159. {
  160. if (lockHeld)
  161. {
  162. Msmq.LeaveXPSendLock();
  163. }
  164. }
  165. }
  166. }
  167. class MsmqIntegrationOutputMessage : MsmqOutputMessage<IOutputChannel>
  168. {
  169. ByteProperty acknowledge;
  170. StringProperty adminQueue;
  171. IntProperty appSpecific;
  172. BufferProperty correlationId;
  173. BufferProperty extension;
  174. StringProperty label;
  175. ByteProperty priority;
  176. StringProperty responseQueue;
  177. public MsmqIntegrationOutputMessage(
  178. MsmqChannelFactoryBase<IOutputChannel> factory,
  179. int bodySize,
  180. EndpointAddress remoteAddress,
  181. MsmqIntegrationMessageProperty property)
  182. : base(factory, bodySize, remoteAddress, 8)
  183. {
  184. if (null == property)
  185. {
  186. Fx.Assert("MsmqIntegrationMessageProperty expected");
  187. }
  188. if (property.AcknowledgeType.HasValue)
  189. EnsureAcknowledgeProperty((byte)property.AcknowledgeType.Value);
  190. if (null != property.AdministrationQueue)
  191. EnsureAdminQueueProperty(property.AdministrationQueue, false);
  192. if (property.AppSpecific.HasValue)
  193. this.appSpecific = new IntProperty(this, UnsafeNativeMethods.PROPID_M_APPSPECIFIC, property.AppSpecific.Value);
  194. if (property.BodyType.HasValue)
  195. EnsureBodyTypeProperty(property.BodyType.Value);
  196. if (null != property.CorrelationId)
  197. this.correlationId = new BufferProperty(this, UnsafeNativeMethods.PROPID_M_CORRELATIONID, MsmqMessageId.FromString(property.CorrelationId));
  198. if (null != property.Extension)
  199. this.extension = new BufferProperty(this, UnsafeNativeMethods.PROPID_M_EXTENSION, property.Extension);
  200. if (null != property.Label)
  201. this.label = new StringProperty(this, UnsafeNativeMethods.PROPID_M_LABEL, property.Label);
  202. if (property.Priority.HasValue)
  203. this.priority = new ByteProperty(this, UnsafeNativeMethods.PROPID_M_PRIORITY, (byte)property.Priority.Value);
  204. if (null != property.ResponseQueue)
  205. EnsureResponseQueueProperty(property.ResponseQueue);
  206. if (property.TimeToReachQueue.HasValue)
  207. EnsureTimeToReachQueueProperty(MsmqDuration.FromTimeSpan(property.TimeToReachQueue.Value));
  208. }
  209. void EnsureAcknowledgeProperty(byte value)
  210. {
  211. if (this.acknowledge == null)
  212. {
  213. this.acknowledge = new ByteProperty(this, UnsafeNativeMethods.PROPID_M_ACKNOWLEDGE);
  214. }
  215. this.acknowledge.Value = value;
  216. }
  217. void EnsureAdminQueueProperty(Uri value, bool useNetMsmqTranslator)
  218. {
  219. if (null != value)
  220. {
  221. string queueName = useNetMsmqTranslator ?
  222. MsmqUri.NetMsmqAddressTranslator.UriToFormatName(value) :
  223. MsmqUri.FormatNameAddressTranslator.UriToFormatName(value);
  224. if (this.adminQueue == null)
  225. {
  226. this.adminQueue = new StringProperty(this, UnsafeNativeMethods.PROPID_M_ADMIN_QUEUE, queueName);
  227. }
  228. else
  229. {
  230. this.adminQueue.SetValue(queueName);
  231. }
  232. }
  233. }
  234. void EnsureResponseQueueProperty(Uri value)
  235. {
  236. if (null != value)
  237. {
  238. string queueName = MsmqUri.FormatNameAddressTranslator.UriToFormatName(value);
  239. if (this.responseQueue == null)
  240. {
  241. this.responseQueue = new StringProperty(this, UnsafeNativeMethods.PROPID_M_RESP_FORMAT_NAME, queueName);
  242. }
  243. else
  244. {
  245. this.responseQueue.SetValue(queueName);
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }