2
0

MessageProcessingContext.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. namespace System.ServiceModel.Dispatcher
  7. {
  8. internal class MessageProcessingContext
  9. {
  10. OperationContext operation_context;
  11. RequestContext request_context;
  12. Message incoming_message;
  13. IDefaultCommunicationTimeouts timeouts;
  14. Message reply_message;
  15. InstanceContext instance_context;
  16. Exception processingException;
  17. DispatchOperation operation;
  18. UserEventsHandler user_events_handler;
  19. public MessageProcessingContext (OperationContext opCtx)
  20. {
  21. operation_context = opCtx;
  22. request_context = opCtx.RequestContext;
  23. incoming_message = opCtx.IncomingMessage;
  24. timeouts = opCtx.CommunicationTimeouts;
  25. user_events_handler = new UserEventsHandler (this);
  26. }
  27. public DispatchOperation Operation
  28. {
  29. get { return operation; }
  30. set { operation = value; }
  31. }
  32. public Exception ProcessingException
  33. {
  34. get { return processingException; }
  35. set { processingException = value; }
  36. }
  37. public Message ReplyMessage
  38. {
  39. get { return reply_message; }
  40. set { reply_message = value; }
  41. }
  42. public IDefaultCommunicationTimeouts CommunicationTimeouts
  43. {
  44. get { return timeouts; }
  45. set { timeouts = value; }
  46. }
  47. public InstanceContext InstanceContext
  48. {
  49. get { return instance_context; }
  50. set { instance_context = value; }
  51. }
  52. public Message IncomingMessage
  53. {
  54. get { return incoming_message; }
  55. set { incoming_message = value; }
  56. }
  57. public RequestContext RequestContext
  58. {
  59. get { return request_context; }
  60. set { request_context = value; }
  61. }
  62. public OperationContext OperationContext
  63. {
  64. get { return operation_context; }
  65. set { operation_context = value; }
  66. }
  67. public UserEventsHandler EventsHandler
  68. {
  69. get { return user_events_handler; }
  70. set { user_events_handler = value; }
  71. }
  72. public void Reply (bool useTimeout)
  73. {
  74. EventsHandler.BeforeSendReply ();
  75. if (useTimeout)
  76. RequestContext.Reply (ReplyMessage, CommunicationTimeouts.SendTimeout);
  77. else
  78. RequestContext.Reply (ReplyMessage);
  79. }
  80. }
  81. #region user events implementation
  82. internal class UserEventsHandler
  83. {
  84. MessageProcessingContext request_context;
  85. DispatchRuntime dispatch_runtime;
  86. IClientChannel channel;
  87. object [] msg_inspectors_states;
  88. object [] callcontext_initializers_states;
  89. public UserEventsHandler (MessageProcessingContext mrc)
  90. {
  91. request_context = mrc;
  92. dispatch_runtime = mrc.OperationContext.EndpointDispatcher.DispatchRuntime;
  93. msg_inspectors_states = new object [dispatch_runtime.MessageInspectors.Count];
  94. channel = request_context.OperationContext.Channel as IClientChannel;
  95. }
  96. public void AfterReceiveRequest ()
  97. {
  98. Message message = request_context.IncomingMessage;
  99. for (int i = 0; i < dispatch_runtime.MessageInspectors.Count; ++i)
  100. msg_inspectors_states [i] = dispatch_runtime.MessageInspectors [i].AfterReceiveRequest (
  101. ref message, channel, request_context.InstanceContext);
  102. request_context.IncomingMessage = message;
  103. }
  104. public void BeforeSendReply ()
  105. {
  106. Message toBeChanged = request_context.ReplyMessage;
  107. for (int i = 0; i < dispatch_runtime.MessageInspectors.Count; ++i)
  108. dispatch_runtime.MessageInspectors [i].BeforeSendReply (ref toBeChanged, msg_inspectors_states [i]);
  109. }
  110. public void BeforeInvoke (DispatchOperation operation)
  111. {
  112. callcontext_initializers_states = new object [operation.CallContextInitializers.Count];
  113. for (int i = 0; i < callcontext_initializers_states.Length; ++i)
  114. callcontext_initializers_states [i] = operation.CallContextInitializers [i].BeforeInvoke (
  115. request_context.InstanceContext, channel, request_context.IncomingMessage);
  116. }
  117. public void AfterInvoke (DispatchOperation operation)
  118. {
  119. for (int i = 0; i < callcontext_initializers_states.Length; ++i)
  120. operation.CallContextInitializers [i].AfterInvoke (callcontext_initializers_states [i]);
  121. }
  122. }
  123. #endregion
  124. }