none
IParameterInspector AfterCall method - immutable string

    Question

  • Hello,

    I need to use the AfterCall method of IParameterInspector on a WCF service in order to automatically change my return values of all service operations in some situations. Here is the code i have written:

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
                returnValue = UtilMethods.ChangeObject(returnValue);
                Debug.WriteLine(returnValue.ToString());
    }

    In the output window, Debug.WriteLine prints the changed value as expected. But in case the return value of an operation is of type String, though in the output window it prints the changed value, further on the channel goes the previous, unchanged value. I tested this with Fiddler and i clearly see that when returning string the value does not change.

    I guess this has to do with the fact that strings are immutable in .NET, though being of reference type, so i am actually changing a copy of the string value which is to be passed over the channel. My question is this: without changing my operations' signatures, how can i resolve this situation?

    Thank you,
    Mihai 

    Saturday, March 19, 2011 12:21 PM

Answers

  • Hi Mihai

    As Luo told you, you cannot change the return value in a parameter inspector because it's not passed as a reference, and even if it was you’d be in trouble with boxed value types.

    If you really need to inject custom logic in your operation processing, consider replacing the default operation invoker (http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ioperationinvoker.aspx). You can easily write an invoker by encapsulating the system provided one, than in your Invoke implementation (assuming a sync invoker), you can call the encapsulated implementation and then change the operation outputs as you wish.

    Regards


    Antonello Tesoro .Net Enterprise Software Architect
    • Marked as answer by Yi-Lun Luo Friday, March 25, 2011 8:41 AM
    Tuesday, March 22, 2011 11:43 AM

All replies