Saturday, March 19, 2011 12:21 PM
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);
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?
Monday, March 21, 2011 6:47 AMModeratorHello, I don't thinks there's a workaround. You will encounter the same issue if you're using value types (such as int). Usually you won't change return value in a parameter inspector anyway...
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
Windows Azure Technical Forum Support Team Blog
Monday, March 21, 2011 10:08 AM
Usually you don't change return values, but i have a specific situation where i would have liked this option. Anyway, thanks for the reply - we will avoid those situations.
Tuesday, March 22, 2011 11:43 AM
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.
Antonello Tesoro .Net Enterprise Software Architect
- Marked As Answer by Yi-Lun LuoModerator Friday, March 25, 2011 8:41 AM
Tuesday, May 03, 2011 5:55 PMModeratorThe implementation suggested by Antonello can be found at http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/12/wcf-extensibility-ioperationbehavior.aspx.