2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.ServiceModel / System.ServiceModel.Dispatcher / MessageProcessingContext.cs
blob42349e86da6f563dc79f7084373492e24be4bae9
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ServiceModel;
5 using System.ServiceModel.Channels;
7 namespace System.ServiceModel.Dispatcher
9 internal class MessageProcessingContext
11 OperationContext operation_context;
12 RequestContext request_context;
13 Message incoming_message;
15 Message reply_message;
16 InstanceContext instance_context;
17 Exception processingException;
18 DispatchOperation operation;
19 UserEventsHandler user_events_handler;
21 public MessageProcessingContext (OperationContext opCtx)
23 operation_context = opCtx;
24 request_context = opCtx.RequestContext;
25 incoming_message = opCtx.IncomingMessage;
26 user_events_handler = new UserEventsHandler (this);
29 public DispatchOperation Operation
31 get { return operation; }
32 set { operation = value; }
35 public Exception ProcessingException
37 get { return processingException; }
38 set { processingException = value; }
41 public Message ReplyMessage
43 get { return reply_message; }
44 set { reply_message = value; }
47 public InstanceContext InstanceContext
49 get { return instance_context; }
50 set { instance_context = value; }
53 public Message IncomingMessage
55 get { return incoming_message; }
56 set { incoming_message = value; }
59 public RequestContext RequestContext
61 get { return request_context; }
62 set { request_context = value; }
65 public OperationContext OperationContext
67 get { return operation_context; }
68 set { operation_context = value; }
71 public UserEventsHandler EventsHandler
73 get { return user_events_handler; }
74 set { user_events_handler = value; }
77 public void Reply (IDuplexChannel channel, bool useTimeout)
79 EventsHandler.BeforeSendReply ();
80 if (useTimeout)
81 channel.Send (ReplyMessage, Operation.Parent.ChannelDispatcher.timeouts.SendTimeout);
82 else
83 channel.Send (ReplyMessage);
86 public void Reply (bool useTimeout)
88 EventsHandler.BeforeSendReply ();
89 if (useTimeout)
90 RequestContext.Reply (ReplyMessage, Operation.Parent.ChannelDispatcher.timeouts.SendTimeout);
91 else
92 RequestContext.Reply (ReplyMessage);
96 #region user events implementation
98 internal class UserEventsHandler
100 MessageProcessingContext request_context;
101 DispatchRuntime dispatch_runtime;
102 IClientChannel channel;
103 object [] msg_inspectors_states;
104 object [] callcontext_initializers_states;
106 public UserEventsHandler (MessageProcessingContext mrc)
108 request_context = mrc;
109 dispatch_runtime = mrc.OperationContext.EndpointDispatcher.DispatchRuntime;
110 msg_inspectors_states = new object [dispatch_runtime.MessageInspectors.Count];
111 channel = request_context.OperationContext.Channel as IClientChannel;
114 public void AfterReceiveRequest ()
116 Message message = request_context.IncomingMessage;
118 for (int i = 0; i < dispatch_runtime.MessageInspectors.Count; ++i)
119 msg_inspectors_states [i] = dispatch_runtime.MessageInspectors [i].AfterReceiveRequest (
120 ref message, channel, request_context.InstanceContext);
121 request_context.IncomingMessage = message;
125 public void BeforeSendReply ()
127 Message toBeChanged = request_context.ReplyMessage;
128 for (int i = 0; i < dispatch_runtime.MessageInspectors.Count; ++i)
129 dispatch_runtime.MessageInspectors [i].BeforeSendReply (ref toBeChanged, msg_inspectors_states [i]);
132 public void BeforeInvoke (DispatchOperation operation)
134 callcontext_initializers_states = new object [operation.CallContextInitializers.Count];
135 for (int i = 0; i < callcontext_initializers_states.Length; ++i)
136 callcontext_initializers_states [i] = operation.CallContextInitializers [i].BeforeInvoke (
137 request_context.InstanceContext, channel, request_context.IncomingMessage);
141 public void AfterInvoke (DispatchOperation operation)
143 for (int i = 0; i < callcontext_initializers_states.Length; ++i)
144 operation.CallContextInitializers [i].AfterInvoke (callcontext_initializers_states [i]);
148 #endregion