OperationInvokerBehavior.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Description;
  9. class OperationInvokerBehavior : IOperationBehavior
  10. {
  11. public OperationInvokerBehavior()
  12. {
  13. }
  14. void IOperationBehavior.Validate(OperationDescription description)
  15. {
  16. }
  17. void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
  18. {
  19. }
  20. void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
  21. {
  22. if (dispatch == null)
  23. {
  24. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatch");
  25. }
  26. if (description == null)
  27. {
  28. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  29. }
  30. if (description.TaskMethod != null)
  31. {
  32. dispatch.Invoker = new TaskMethodInvoker(description.TaskMethod, description.TaskTResult);
  33. }
  34. else if (description.SyncMethod != null)
  35. {
  36. if (description.BeginMethod != null)
  37. {
  38. // both [....] and async methods are present on the contract, check the preference
  39. OperationBehaviorAttribute operationBehaviorAttribue = description.Behaviors.Find<OperationBehaviorAttribute>();
  40. if ((operationBehaviorAttribue != null) && operationBehaviorAttribue.PreferAsyncInvocation)
  41. {
  42. dispatch.Invoker = new AsyncMethodInvoker(description.BeginMethod, description.EndMethod);
  43. }
  44. else
  45. {
  46. dispatch.Invoker = new SyncMethodInvoker(description.SyncMethod);
  47. }
  48. }
  49. else
  50. {
  51. // only [....] method is present on the contract
  52. dispatch.Invoker = new SyncMethodInvoker(description.SyncMethod);
  53. }
  54. }
  55. else
  56. {
  57. if (description.BeginMethod != null)
  58. {
  59. // only async method is present on the contract
  60. dispatch.Invoker = new AsyncMethodInvoker(description.BeginMethod, description.EndMethod);
  61. }
  62. }
  63. }
  64. void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
  65. {
  66. }
  67. }
  68. }