I am looking for a way to set up some state in thread local storage immediately before operations are invoked and then to clean it up immediately after they have completed. (i.e. the pre/post operation code must run on the thread used to service the request).
What would be the best way to do this
Thanks.

Pre/Post Operation Invokation Extensibility
Potyos
Great - thanks for the answer.
Can anyone confirm if:
Also, (using the example from the previous post) under what circumstances (if any) could the finally block be run on a different thread from the try block in the following:
try {
// do setup
object retval = innerInvoker.Invoke(...)
}
finally {
// do cleanup
}
Thanks.
Ross Goodell
Also,
If you need custom state that can be called from within any method during the lifetime of the operation you could implement the IExtension<OperationContext> interface and add it via step 2 in Roman's description above.
http://windowscommunication.net/ControlGallery/ControlDetail.aspx Control=2241&tabindex=2
Thanks,
Scott
Tiago Teixeira
You can implement the IDispatchOperationSelector interface.
Thanks!
Scott
Jag Sandhu
Scott,
Can you forward me the sample gkicha ---at--- gmail ----dot----- com I'm looking for a very similar capability. Does it work on RC0
Thanks.
Kishore
Dgates123
Frank,
The IOperationBehavior and IOperationInvoker interfaces will do it for you.
The following are the major steps:
1. Create your Invoker class derived from IOperationInvoker and implement its method
object IOperationInvoker.Invoke(object instance, object[] inputs, out object[] outputs)
{
// todo: - before
object retval = this.innerOperationInvoker.Invoke(instance, inputs, out outputs);
// todo: after
return retval;
}
2. Create attribute class derived from Attribute and IOperationBehavior and implement its interface method, for instance like the following example:
public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
{
dispatch.Invoker = new MyInvoker( ...);
}
3. Dekorate your service method(s) with your attribute
You can find more implementation details about this solution in my article WS-Transfer located in the Sample Gallery.
Thanks
Roman
nka4
Yes it is possible, if the return object and outputs are the same type as original.
Roman
tobyxu
Thanks for the response.
For the scenario I have in mind (something along the lines of an [AutoEnlistUnitOfWorkAttribute] with a static UnitOfWork.Current property that retrieves the UnitOfWork instance from a LocalDataStoreSlot). Code in another assembly needs to see UnitOfWork.Current, but I would rather not introduce a dependency on System.ServiceModel.
maixmus
Diego Ojeda
I have sample code that demos this capability using soap headers to direct which method to call on the server. The client only knows of one method.
Abha, I'll forward the sample to you offline.
Thanks,
Scott