Pre/Post Operation Invokation Extensibility

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.



Answer this question

Pre/Post Operation Invokation Extensibility

  • Markus_R

    Is it possible to change the method that is being invoked here I mean say on the basis of pre-processing I want to call another method on the same srevice

  • AbhishekVerma

    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



  • Sweeps78

    Great - thanks for the answer.

    Can anyone confirm if:

    1. the thread on which a single operation request executes is only ever a single thread (unless you create additional threads yourself); and
    2. that this thread is never used for anything else during the lifetime of the execution of the operation; and
    3. (1) and (2) remain true if hosted within an ASP.NET application; and
    4. this is all regardless of the ConcurrencyMode

    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.


  • Chandra Sekhar M

    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



  • George Jodry

    You can implement the IDispatchOperationSelector interface.

    Thanks!

    Scott



  • Chaun Goins

    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.


  • doonavin

    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



  • rkdayan

    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


  • JayUK

     

    Yes it is possible, if the return object and outputs are the same type as original.

     

    Roman



  • Pre/Post Operation Invokation Extensibility