ContextRequestChannelBase.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. abstract class ContextRequestChannelBase<TChannel> : LayeredChannel<TChannel> where TChannel : class, IRequestChannel
  10. {
  11. ContextProtocol contextProtocol;
  12. protected ContextRequestChannelBase(ChannelManagerBase channelManager, TChannel innerChannel,
  13. ContextExchangeMechanism contextExchangeMechanism, Uri callbackAddress, bool contextManagementEnabled)
  14. : base(channelManager, innerChannel)
  15. {
  16. this.contextProtocol = new ClientContextProtocol(contextExchangeMechanism, innerChannel.Via, this, callbackAddress, contextManagementEnabled);
  17. }
  18. public EndpointAddress RemoteAddress
  19. {
  20. get { return this.InnerChannel.RemoteAddress; }
  21. }
  22. public Uri Via
  23. {
  24. get { return this.InnerChannel.Via; }
  25. }
  26. public IAsyncResult BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, object state)
  27. {
  28. this.contextProtocol.OnOutgoingMessage(message, null);
  29. return new RequestAsyncResult(message, this.InnerChannel, timeout, callback, state);
  30. }
  31. public IAsyncResult BeginRequest(Message message, AsyncCallback callback, object state)
  32. {
  33. return this.BeginRequest(message, this.DefaultSendTimeout, callback, state);
  34. }
  35. public Message EndRequest(IAsyncResult result)
  36. {
  37. Message message = RequestAsyncResult.End(result);
  38. if (message != null)
  39. {
  40. this.contextProtocol.OnIncomingMessage(message);
  41. }
  42. return message;
  43. }
  44. public override T GetProperty<T>()
  45. {
  46. if (typeof(T) == typeof(IContextManager) && this.contextProtocol is IContextManager)
  47. {
  48. return (T)(object)this.contextProtocol;
  49. }
  50. else
  51. {
  52. return base.GetProperty<T>();
  53. }
  54. }
  55. public Message Request(Message message, TimeSpan timeout)
  56. {
  57. CorrelationCallbackMessageProperty callback = null;
  58. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  59. Message requestMessage = message;
  60. this.contextProtocol.OnOutgoingMessage(message, null);
  61. if (message != null && CorrelationCallbackMessageProperty.TryGet(message, out callback))
  62. {
  63. ContextExchangeCorrelationHelper.AddOutgoingCorrelationCallbackData(callback, message, true);
  64. if (callback.IsFullyDefined)
  65. {
  66. requestMessage = callback.FinalizeCorrelation(message, timeoutHelper.RemainingTime());
  67. }
  68. }
  69. Message response = null;
  70. try
  71. {
  72. response = this.InnerChannel.Request(requestMessage, timeout);
  73. if (response != null)
  74. {
  75. this.contextProtocol.OnIncomingMessage(response);
  76. }
  77. }
  78. finally
  79. {
  80. if (message != null && !object.ReferenceEquals(message, requestMessage))
  81. {
  82. requestMessage.Close();
  83. }
  84. }
  85. return response;
  86. }
  87. public Message Request(Message message)
  88. {
  89. return this.Request(message, this.DefaultSendTimeout);
  90. }
  91. class RequestAsyncResult : AsyncResult
  92. {
  93. static AsyncCallback onFinalizeCorrelation = Fx.ThunkCallback(new AsyncCallback(OnFinalizeCorrelationCompletedCallback));
  94. static AsyncCallback onRequest = Fx.ThunkCallback(new AsyncCallback(OnRequestCompletedCallback));
  95. IRequestChannel channel;
  96. CorrelationCallbackMessageProperty correlationCallback;
  97. Message message;
  98. Message replyMessage;
  99. Message requestMessage;
  100. TimeoutHelper timeoutHelper;
  101. public RequestAsyncResult(Message message, IRequestChannel channel, TimeSpan timeout, AsyncCallback callback, object state)
  102. : base(callback, state)
  103. {
  104. this.channel = channel;
  105. this.message = this.requestMessage = message;
  106. this.timeoutHelper = new TimeoutHelper(timeout);
  107. bool shouldRequest = true;
  108. if (message != null)
  109. {
  110. if (CorrelationCallbackMessageProperty.TryGet(message, out this.correlationCallback))
  111. {
  112. ContextExchangeCorrelationHelper.AddOutgoingCorrelationCallbackData(this.correlationCallback, message, true);
  113. if (this.correlationCallback.IsFullyDefined)
  114. {
  115. IAsyncResult result = this.correlationCallback.BeginFinalizeCorrelation(this.message, this.timeoutHelper.RemainingTime(), onFinalizeCorrelation, this);
  116. if (result.CompletedSynchronously)
  117. {
  118. if (OnFinalizeCorrelationCompleted(result))
  119. {
  120. base.Complete(true);
  121. }
  122. }
  123. shouldRequest = false;
  124. }
  125. }
  126. }
  127. if (shouldRequest)
  128. {
  129. IAsyncResult result = this.channel.BeginRequest(this.message, this.timeoutHelper.RemainingTime(), onRequest, this);
  130. if (result.CompletedSynchronously)
  131. {
  132. OnRequestCompleted(result);
  133. base.Complete(true);
  134. }
  135. }
  136. }
  137. public static Message End(IAsyncResult result)
  138. {
  139. RequestAsyncResult thisPtr = AsyncResult.End<RequestAsyncResult>(result);
  140. return thisPtr.replyMessage;
  141. }
  142. static void OnFinalizeCorrelationCompletedCallback(IAsyncResult result)
  143. {
  144. if (result.CompletedSynchronously)
  145. {
  146. return;
  147. }
  148. RequestAsyncResult thisPtr = (RequestAsyncResult)result.AsyncState;
  149. Exception completionException = null;
  150. bool completeSelf;
  151. try
  152. {
  153. completeSelf = thisPtr.OnFinalizeCorrelationCompleted(result);
  154. }
  155. catch (Exception e)
  156. {
  157. if (Fx.IsFatal(e))
  158. {
  159. throw;
  160. }
  161. completionException = e;
  162. completeSelf = true;
  163. }
  164. if (completeSelf)
  165. {
  166. thisPtr.Complete(false, completionException);
  167. }
  168. }
  169. static void OnRequestCompletedCallback(IAsyncResult result)
  170. {
  171. if (result.CompletedSynchronously)
  172. {
  173. return;
  174. }
  175. RequestAsyncResult thisPtr = (RequestAsyncResult)result.AsyncState;
  176. Exception completionException = null;
  177. try
  178. {
  179. thisPtr.OnRequestCompleted(result);
  180. }
  181. catch (Exception e)
  182. {
  183. if (Fx.IsFatal(e))
  184. {
  185. throw;
  186. }
  187. completionException = e;
  188. }
  189. thisPtr.Complete(false, completionException);
  190. }
  191. bool OnFinalizeCorrelationCompleted(IAsyncResult result)
  192. {
  193. this.requestMessage = this.correlationCallback.EndFinalizeCorrelation(result);
  194. this.requestMessage.Properties.Remove(CorrelationCallbackMessageProperty.Name);
  195. bool throwing = true;
  196. IAsyncResult requestResult;
  197. try
  198. {
  199. requestResult = this.channel.BeginRequest(this.requestMessage, this.timeoutHelper.RemainingTime(), onRequest, this);
  200. throwing = false;
  201. }
  202. finally
  203. {
  204. if (throwing)
  205. {
  206. if (this.message != null && !object.ReferenceEquals(this.message, this.requestMessage))
  207. {
  208. this.requestMessage.Close();
  209. }
  210. }
  211. }
  212. if (requestResult.CompletedSynchronously)
  213. {
  214. OnRequestCompleted(requestResult);
  215. return true;
  216. }
  217. return false;
  218. }
  219. void OnRequestCompleted(IAsyncResult result)
  220. {
  221. try
  222. {
  223. this.replyMessage = this.channel.EndRequest(result);
  224. }
  225. finally
  226. {
  227. if (this.message != null && !object.ReferenceEquals(this.message, this.requestMessage))
  228. {
  229. this.requestMessage.Close();
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }