/src/Castle.Facilities.WcfIntegration/Client/Async/WcfAsync.cs

https://github.com/castleproject/Windsor · C# · 299 lines · 113 code · 20 blank · 166 comment · 4 complexity · 2ed485a7af91950dabb40437515184e4 MD5 · raw file

  1. // Copyright 2004-2011 Castle Project - http://www.castleproject.org/
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. namespace Castle.Facilities.WcfIntegration
  15. {
  16. using System;
  17. using Castle.Facilities.WcfIntegration.Async;
  18. public static class WcfAsync
  19. {
  20. private static readonly AsyncCallback Nothing = delegate { };
  21. /// <summary>
  22. /// Begins an asynchronous call of <paramref name="method"/> on given <paramref name="proxy"/>.
  23. /// </summary>
  24. /// <typeparam name="TProxy">The type of the proxy.</typeparam>
  25. /// <typeparam name="TResult">The type of the result.</typeparam>
  26. /// <param name="proxy">The proxy.</param>
  27. /// <param name="method">The delegate encapsulating the invocation of the method.</param>
  28. /// <returns>The async call handle.</returns>
  29. public static IWcfAsyncCall<TResult> BeginWcfCall<TProxy, TResult>(
  30. this TProxy proxy, Func<TProxy, TResult> method)
  31. {
  32. return BeginWcfCall(proxy, method, Nothing, null);
  33. }
  34. /// <summary>
  35. /// Begins an asynchronous call of <paramref name="method"/> on given <paramref name="proxy"/>.
  36. /// </summary>
  37. /// <typeparam name="TProxy">The type of the proxy.</typeparam>
  38. /// <typeparam name="TResult">The type of the result.</typeparam>
  39. /// <param name="proxy">The proxy.</param>
  40. /// <param name="method">The delegate encapsulating the invocation of the method.</param>
  41. /// <param name="callback">The asynchronous callback.</param>
  42. /// <param name="state">The asynchronous state.</param>
  43. /// <returns>The async call handle.</returns>
  44. public static IWcfAsyncCall<TResult> BeginWcfCall<TProxy, TResult>(
  45. this TProxy proxy, Func<TProxy, TResult> method,
  46. Action<IWcfAsyncCall<TResult>> callback, object state)
  47. {
  48. var call = new AsyncWcfCall<TProxy, TResult>(proxy, method);
  49. call.Begin(ar => callback(call), state);
  50. return call;
  51. }
  52. /// <summary>
  53. /// Begins an asynchronous call of <paramref name="method"/> on given <paramref name="proxy"/>.
  54. /// </summary>
  55. /// <typeparam name="TProxy">The type of the proxy.</typeparam>
  56. /// <typeparam name="TResult">The type of the result.</typeparam>
  57. /// <param name="proxy">The proxy.</param>
  58. /// <param name="method">The delegate encapsulating the invocation of the method.</param>
  59. /// <param name="callback">The asynchronous callback.</param>
  60. /// <param name="state">The asynchronous state.</param>
  61. /// <returns>The async call handle.</returns>
  62. public static IWcfAsyncCall<TResult> BeginWcfCall<TProxy, TResult>(
  63. this TProxy proxy, Func<TProxy, TResult> method,
  64. AsyncCallback callback, object state)
  65. {
  66. var call = new AsyncWcfCall<TProxy, TResult>(proxy, method);
  67. call.Begin(ar => callback(call), state);
  68. return call;
  69. }
  70. /// <summary>
  71. /// Ends the asynchronous call and returns the result.
  72. /// </summary>
  73. /// <typeparam name="TResult">The result type.</typeparam>
  74. /// <param name="proxy">The proxy.</param>
  75. /// <param name="result">The asynchronous result.</param>
  76. /// <returns>The result of the call.</returns>
  77. public static TResult EndWcfCall<TResult>(this object proxy, IAsyncResult result)
  78. {
  79. return EnsureAsyncCall<TResult>(result).End();
  80. }
  81. /// <summary>
  82. /// Ends the asynchronous call and returns the results.
  83. /// </summary>
  84. /// <typeparam name="TResult">The main result type.</typeparam>
  85. /// <typeparam name="TOut1">The additional result type.</typeparam>
  86. /// <param name="proxy">The proxy.</param>
  87. /// <param name="result">The asynchronous result.</param>
  88. /// <param name="out1">The additional result.</param>
  89. /// <returns>The main result.</returns>
  90. public static TResult EndWcfCall<TResult, TOut1>(this object proxy, IAsyncResult result, out TOut1 out1)
  91. {
  92. return EnsureAsyncCall<TResult>(result).End(out out1);
  93. }
  94. /// <summary>
  95. /// Ends the asynchronous call and returns the results.
  96. /// </summary>
  97. /// <typeparam name="TResult">The main result type.</typeparam>
  98. /// <typeparam name="TOut1">The additional result type.</typeparam>
  99. /// <typeparam name="TOut2">The additional result type.</typeparam>
  100. /// <param name="proxy">The proxy.</param>
  101. /// <param name="result">The asynchronous result.</param>
  102. /// <param name="out1">The first additional result.</param>
  103. /// <param name="out2">The second additional result.</param>
  104. /// <returns>The main result.</returns>
  105. public static TResult EndWcfCall<TResult, TOut1, TOut2>(
  106. this object proxy, IAsyncResult result, out TOut1 out1, TOut2 out2)
  107. {
  108. return EnsureAsyncCall<TResult>(result).End(out out1, out out2);
  109. }
  110. /// <summary>
  111. /// Ends the asynchronous call and returns the results.
  112. /// </summary>
  113. /// <typeparam name="TResult">The main result type.</typeparam>
  114. /// <typeparam name="TOut1">The additional result type.</typeparam>
  115. /// <typeparam name="TOut2">The additional result type.</typeparam>
  116. /// <typeparam name="TOut3">The additional result type.</typeparam>
  117. /// <param name="proxy">The proxy.</param>
  118. /// <param name="result">The asynchronous result.</param>
  119. /// <param name="out1">The first additional result.</param>
  120. /// <param name="out2">The second additional result.</param>
  121. /// <param name="out3">The thrid additional result.</param>
  122. /// <returns>The main result.</returns>
  123. public static TResult EndWcfCall<TResult, TOut1, TOut2, TOut3>(
  124. this object proxy, IAsyncResult result, out TOut1 out1,TOut2 out2, TOut3 out3)
  125. {
  126. return EnsureAsyncCall<TResult>(result).End(out out1, out out2, out out3);
  127. }
  128. /// <summary>
  129. /// Ends the asynchronous call and returns the results.
  130. /// </summary>
  131. /// <typeparam name="TResult">The main result type.</typeparam>
  132. /// <typeparam name="TOut1">he additional result type.</typeparam>
  133. /// <typeparam name="TOut2">The additional result type.</typeparam>
  134. /// <typeparam name="TOut3">The additional result type.</typeparam>
  135. /// <typeparam name="TOut4">The additional result type.</typeparam>
  136. /// <param name="proxy">The proxy.</param>
  137. /// <param name="result">The asynchronous result.</param>
  138. /// <param name="out1">The first additional result.</param>
  139. /// <param name="out2">The second additional result.</param>
  140. /// <param name="out3">The third additional result.</param>
  141. /// <param name="out4">The fourth additional result.</param>
  142. /// <returns>The main result.</returns>
  143. public static TResult EndWcfCall<TResult, TOut1, TOut2, TOut3, TOut4>(
  144. this object proxy, IAsyncResult result, out TOut1 out1, TOut2 out2, TOut3 out3, TOut4 out4)
  145. {
  146. return EnsureAsyncCall<TResult>(result).End(out out1, out out2, out out3, out out4);
  147. }
  148. /// <summary>
  149. /// Begins an asynchronous call of <paramref name="method"/> on given <paramref name="proxy"/>.
  150. /// </summary>
  151. /// <typeparam name="TProxy">The type of the proxy.</typeparam>
  152. /// <param name="proxy">The proxy.</param>
  153. /// <param name="method">The delegate encapsulating the invocation of the method.</param>
  154. /// <returns>The async call handle.</returns>
  155. public static IWcfAsyncCall BeginWcfCall<TProxy>(this TProxy proxy, Action<TProxy> method)
  156. {
  157. return BeginWcfCall(proxy, method, Nothing, null);
  158. }
  159. /// <summary>
  160. /// Begins an asynchronous call of <paramref name="method"/> on given <paramref name="proxy"/>.
  161. /// </summary>
  162. /// <typeparam name="TProxy">The type of the proxy.</typeparam>
  163. /// <param name="proxy">The proxy.</param>
  164. /// <param name="method">The delegate encapsulating the invocation of the method.</param>
  165. /// <param name="callback">The asynchronous callback.</param>
  166. /// <param name="state">The asynchronous state.</param>
  167. /// <returns>The async call handle.</returns>
  168. public static IWcfAsyncCall BeginWcfCall<TProxy>(this TProxy proxy, Action<TProxy> method,
  169. Action<IWcfAsyncCall> callback, object state)
  170. {
  171. var call = new AsyncWcfCall<TProxy>(proxy, method);
  172. call.Begin(ar => callback(call), state);
  173. return call;
  174. }
  175. /// <summary>
  176. /// Begins an asynchronous call of <paramref name="method"/> on given <paramref name="proxy"/>.
  177. /// </summary>
  178. /// <typeparam name="TProxy">The type of the proxy.</typeparam>
  179. /// <param name="proxy">The proxy.</param>
  180. /// <param name="method">The delegate encapsulating the invocation of the method.</param>
  181. /// <param name="callback">The asynchronous callback.</param>
  182. /// <param name="state">The asynchronous state.</param>
  183. /// <returns>The async call handle.</returns>
  184. public static IWcfAsyncCall BeginWcfCall<TProxy>(this TProxy proxy, Action<TProxy> method,
  185. AsyncCallback callback, object state)
  186. {
  187. var call = new AsyncWcfCall<TProxy>(proxy, method);
  188. call.Begin(ar => callback(call), state);
  189. return call;
  190. }
  191. /// <summary>
  192. /// Ends the asynchronous call.
  193. /// </summary>
  194. /// <param name="proxy">The proxy.</param>
  195. /// <param name="result">The asynchronous result.</param>
  196. public static void EndWcfCall(this object proxy, IAsyncResult result)
  197. {
  198. EnsureAsyncCall(result).End();
  199. }
  200. /// <summary>
  201. /// Ends the asynchronous call and returns the results.
  202. /// </summary>
  203. /// <typeparam name="TOut1">The additional result type.</typeparam>
  204. /// <param name="proxy">The proxy.</param>
  205. /// <param name="result">The asynchronous result.</param>
  206. /// <param name="out1">The additional result.</param>
  207. public static void EndWcfCall<TOut1>(this object proxy, IAsyncResult result, out TOut1 out1)
  208. {
  209. EnsureAsyncCall(result).End(out out1);
  210. }
  211. /// <summary>
  212. /// Ends the asynchronous call and returns the results.
  213. /// </summary>
  214. /// <typeparam name="TOut1">The additional result type.</typeparam>
  215. /// <typeparam name="TOut2">The additional result type.</typeparam>
  216. /// <param name="proxy">The proxy.</param>
  217. /// <param name="result">The asynchronous result.</param>
  218. /// <param name="out1">The additional result.</param>
  219. /// <param name="out2">The additional result.</param>
  220. public static void EndWcfCall<TOut1, TOut2>(this object proxy, IAsyncResult result, out TOut1 out1, TOut2 out2)
  221. {
  222. EnsureAsyncCall(result).End(out out1, out out2);
  223. }
  224. /// <summary>
  225. /// Ends the asynchronous call and returns the results.
  226. /// </summary>
  227. /// <typeparam name="TOut1">The additional result type.</typeparam>
  228. /// <typeparam name="TOut2">The additional result type.</typeparam>
  229. /// <typeparam name="TOut3">The additional result type.</typeparam>
  230. /// <param name="proxy">The proxy.</param>
  231. /// <param name="result">The asynchronous result.</param>
  232. /// <param name="out1">The additional result.</param>
  233. /// <param name="out2">The additional result.</param>
  234. /// <param name="out3">The additional result.</param>
  235. public static void EndWcfCall<TOut1, TOut2, TOut3>(
  236. this object proxy, IAsyncResult result, out TOut1 out1, TOut2 out2, TOut3 out3)
  237. {
  238. EnsureAsyncCall(result).End(out out1, out out2, out out3);
  239. }
  240. /// <summary>
  241. /// Ends the asynchronous call and returns the results.
  242. /// </summary>
  243. /// <typeparam name="TOut1">The additional result type.</typeparam>
  244. /// <typeparam name="TOut2">The additional result type.</typeparam>
  245. /// <typeparam name="TOut3">The additional result type.</typeparam>
  246. /// <typeparam name="TOut4">The additional result type.</typeparam>
  247. /// <param name="proxy">The proxy.</param>
  248. /// <param name="result">The asynchronous result.</param>
  249. /// <param name="out1">The additional result.</param>
  250. /// <param name="out2">The additional result.</param>
  251. /// <param name="out3">The additional result.</param>
  252. /// <param name="out4">The additional result.</param>
  253. public static void EndWcfCall<TOut1, TOut2, TOut3, TOut4>(
  254. this object proxy, IAsyncResult result, out TOut1 out1, TOut2 out2, TOut3 out3, TOut4 out4)
  255. {
  256. EnsureAsyncCall(result).End(out out1, out out2, out out3, out out4);
  257. }
  258. private static IWcfAsyncCall<TResult> EnsureAsyncCall<TResult>(IAsyncResult result)
  259. {
  260. var call = result as IWcfAsyncCall<TResult>;
  261. if (call == null)
  262. {
  263. throw new ArgumentException("The supplied result is not the correct type. " +
  264. " Did you obtain it from a call to BeginWcfCall<TResult>?");
  265. }
  266. return call;
  267. }
  268. private static IWcfAsyncCall EnsureAsyncCall(IAsyncResult result)
  269. {
  270. var call = result as IWcfAsyncCall;
  271. if (call == null)
  272. {
  273. throw new ArgumentException("The supplied result is not the correct type. " +
  274. " Did you obtain it from a call to BeginWcfCall?");
  275. }
  276. return call;
  277. }
  278. }
  279. }