OperationSelectorBehavior.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Description;
  9. using System.Collections.Generic;
  10. using System.Collections;
  11. using System.Reflection;
  12. class OperationSelectorBehavior : IContractBehavior
  13. {
  14. void IContractBehavior.Validate(ContractDescription description, ServiceEndpoint endpoint)
  15. {
  16. }
  17. void IContractBehavior.AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, BindingParameterCollection parameters)
  18. {
  19. }
  20. void IContractBehavior.ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch)
  21. {
  22. if (dispatch.ClientRuntime != null)
  23. dispatch.ClientRuntime.OperationSelector = new MethodInfoOperationSelector(description, MessageDirection.Output);
  24. }
  25. void IContractBehavior.ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime proxy)
  26. {
  27. proxy.OperationSelector = new MethodInfoOperationSelector(description, MessageDirection.Input);
  28. }
  29. internal class MethodInfoOperationSelector : IClientOperationSelector
  30. {
  31. Dictionary<object, string> operationMap;
  32. internal MethodInfoOperationSelector(ContractDescription description, MessageDirection directionThatRequiresClientOpSelection)
  33. {
  34. operationMap = new Dictionary<object, string>();
  35. for (int i = 0; i < description.Operations.Count; i++)
  36. {
  37. OperationDescription operation = description.Operations[i];
  38. if (operation.Messages[0].Direction == directionThatRequiresClientOpSelection)
  39. {
  40. if (operation.SyncMethod != null)
  41. {
  42. if (!operationMap.ContainsKey(operation.SyncMethod.MethodHandle))
  43. operationMap.Add(operation.SyncMethod.MethodHandle, operation.Name);
  44. }
  45. if (operation.BeginMethod != null)
  46. {
  47. if (!operationMap.ContainsKey(operation.BeginMethod.MethodHandle))
  48. {
  49. operationMap.Add(operation.BeginMethod.MethodHandle, operation.Name);
  50. operationMap.Add(operation.EndMethod.MethodHandle, operation.Name);
  51. }
  52. }
  53. if (operation.TaskMethod != null)
  54. {
  55. if (!operationMap.ContainsKey(operation.TaskMethod.MethodHandle))
  56. {
  57. operationMap.Add(operation.TaskMethod.MethodHandle, operation.Name);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. public bool AreParametersRequiredForSelection
  64. {
  65. get { return false; }
  66. }
  67. public string SelectOperation(MethodBase method, object[] parameters)
  68. {
  69. if (this.operationMap.ContainsKey(method.MethodHandle))
  70. return operationMap[method.MethodHandle];
  71. else
  72. return null;
  73. }
  74. }
  75. }
  76. }