DefaultOperationInvoker.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. public DefaultOperationInvoker (OperationDescription od)
  40. {
  41. this.od = od;
  42. var mi = od.SyncMethod ?? od.BeginMethod;
  43. var il = new List<ParameterInfo> ();
  44. var ol = new List<ParameterInfo> ();
  45. var pl = mi.GetParameters ();
  46. int count = mi == od.BeginMethod ? pl.Length - 2 : pl.Length;
  47. for (int i = 0; i < count; i++) {
  48. var pi = pl [i];
  49. if (!pi.IsOut)
  50. il.Add (pi);
  51. if (pi.IsOut)
  52. ol.Add (pi);
  53. }
  54. in_params = il.ToArray ();
  55. out_params = ol.ToArray ();
  56. }
  57. public bool IsSynchronous {
  58. get { return true; }
  59. }
  60. public object [] AllocateInputs ()
  61. {
  62. return new object [in_params.Length];
  63. }
  64. public object Invoke (object instance, object [] inputs, out object [] outputs)
  65. {
  66. var arr = new object [in_params.Length + out_params.Length];
  67. for (int i = 0; i < in_params.Length; i++)
  68. arr [in_params [i].Position] = inputs [i];
  69. var ret = od.SyncMethod.Invoke (instance, arr);
  70. outputs = new object [out_params.Length];
  71. for (int i = 0; i < out_params.Length; i++)
  72. outputs [i] = arr [out_params [i].Position];
  73. return ret;
  74. }
  75. public IAsyncResult InvokeBegin (object instance, object [] inputs, AsyncCallback callback, object state)
  76. {
  77. var arr = new object [in_params.Length + out_params.Length + 2];
  78. for (int i = 0; i < in_params.Length; i++)
  79. arr [in_params [i].Position] = inputs [i];
  80. arr [arr.Length - 2] = callback;
  81. arr [arr.Length - 1] = state;
  82. return new InvokeAsyncResult (arr, (IAsyncResult) od.BeginMethod.Invoke (instance, arr));
  83. }
  84. public object InvokeEnd (object instance, out object [] outputs, IAsyncResult result)
  85. {
  86. var r = (InvokeAsyncResult) result;
  87. var ret = od.EndMethod.Invoke (instance, new object [] {r.Source});
  88. var arr = r.Parameters;
  89. outputs = new object [out_params.Length];
  90. for (int i = 0; i < out_params.Length; i++)
  91. outputs [i] = arr [out_params [i].Position];
  92. return ret;
  93. }
  94. class InvokeAsyncResult : IAsyncResult
  95. {
  96. public InvokeAsyncResult (object [] parameters, IAsyncResult source)
  97. {
  98. Source = source;
  99. Parameters = parameters;
  100. }
  101. public IAsyncResult Source;
  102. public object [] Parameters;
  103. public WaitHandle AsyncWaitHandle {
  104. get { return Source.AsyncWaitHandle; }
  105. }
  106. public bool CompletedSynchronously {
  107. get { return Source.CompletedSynchronously; }
  108. }
  109. public bool IsCompleted {
  110. get { return Source.IsCompleted; }
  111. }
  112. public object AsyncState {
  113. get { return Source.AsyncState; }
  114. }
  115. }
  116. }
  117. }