Share via


Initializing the Context

Today's article is about a little-known extensibility point that runs shortly before and after a service invocation takes place. The call context initializer allows you to control the state of the thread that will be used for the service call.

 public interface ICallContextInitializer
{
   void AfterInvoke(object correlationState);
   object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message);
}

The context initialize runs on the service and first gets called with BeforeInvoke. BeforeInvoke sets up the context state and returns an optional correlation object. That correlation object gets passed in to AfterInvoke so you can use it however you want during cleanup.

Here's the overall sequence of events relevant to the call context.

1.
Initialize the call context 2. Start impersonating the calling identity 3. Make the call 4. Revert the impersonation 5. Run parameter inspectors on the call output 6. Create the response message 7. Uninitialize the call context

As far as I know, the single use for call context initializers so far is in COM+ integration.

Next time: ICallContextInitializer Example