DefaultOperationInvoker.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // DefaultOperationInvoker.cs
  3. //
  4. // Author: Atsushi Enomoto ([email protected])
  5. //
  6. // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ServiceModel;
  30. using System.ServiceModel.Description;
  31. using System.Reflection;
  32. using System.Threading;
  33. namespace System.ServiceModel.Dispatcher
  34. {
  35. class DefaultOperationInvoker : IOperationInvoker
  36. {
  37. readonly OperationDescription od;
  38. readonly ParameterInfo [] in_params, out_params;
  39. readonly bool is_synchronous;
  40. public DefaultOperationInvoker (OperationDescription od)
  41. {
  42. this.od = od;
  43. is_synchronous = od.SyncMethod != null;
  44. var mi = od.SyncMethod ?? od.BeginMethod;
  45. var il = new List<ParameterInfo> ();
  46. var ol = new List<ParameterInfo> ();
  47. var pl = mi.GetParameters ();
  48. int count = mi == od.BeginMethod ? pl.Length - 2 : pl.Length;
  49. for (int i = 0; i < count; i++) {
  50. var pi = pl [i];
  51. if (!pi.IsOut)
  52. il.Add (pi);
  53. if (pi.IsOut || pi.ParameterType.IsByRef)
  54. ol.Add (pi);
  55. }
  56. in_params = il.ToArray ();
  57. out_params = ol.ToArray ();
  58. }
  59. public bool IsSynchronous {
  60. get { return is_synchronous; }
  61. }
  62. public object [] AllocateInputs ()
  63. {
  64. return new object [in_params.Length];
  65. }
  66. public object Invoke (object instance, object [] inputs, out object [] outputs)
  67. {
  68. var arr = new object [od.SyncMethod.GetParameters ().Length];
  69. for (int i = 0; i < in_params.Length; i++)
  70. arr [in_params [i].Position] = inputs [i];
  71. var ret = od.SyncMethod.Invoke (instance, arr);
  72. outputs = new object [out_params.Length];
  73. for (int i = 0; i < out_params.Length; i++)
  74. outputs [i] = arr [out_params [i].Position];
  75. return ret;
  76. }
  77. public IAsyncResult InvokeBegin (object instance, object [] inputs, AsyncCallback callback, object state)
  78. {
  79. var arr = new object [in_params.Length + out_params.Length + 2];
  80. for (int i = 0; i < in_params.Length; i++)
  81. arr [in_params [i].Position] = inputs [i];
  82. arr [arr.Length - 2] = callback;
  83. arr [arr.Length - 1] = state;
  84. return new InvokeAsyncResult (arr, (IAsyncResult) od.BeginMethod.Invoke (instance, arr));
  85. }
  86. public object InvokeEnd (object instance, out object [] outputs, IAsyncResult result)
  87. {
  88. var r = (InvokeAsyncResult) result;
  89. var ret = od.EndMethod.Invoke (instance, new object [] {r.Source});
  90. var arr = r.Parameters;
  91. outputs = new object [out_params.Length];
  92. for (int i = 0; i < out_params.Length; i++)
  93. outputs [i] = arr [out_params [i].Position];
  94. return ret;
  95. }
  96. class InvokeAsyncResult : IAsyncResult
  97. {
  98. public InvokeAsyncResult (object [] parameters, IAsyncResult source)
  99. {
  100. Source = source;
  101. Parameters = parameters;
  102. }
  103. public IAsyncResult Source;
  104. public object [] Parameters;
  105. public WaitHandle AsyncWaitHandle {
  106. get { return Source.AsyncWaitHandle; }
  107. }
  108. public bool CompletedSynchronously {
  109. get { return Source.CompletedSynchronously; }
  110. }
  111. public bool IsCompleted {
  112. get { return Source.IsCompleted; }
  113. }
  114. public object AsyncState {
  115. get { return Source.AsyncState; }
  116. }
  117. }
  118. }
  119. }