IParameterInspector – How to inspect arguments and return values?

Posted on December 21, 2010 by

0


Overriding the IParameterInspector located in System.ServiceModel.Dispatcher namespace allows inspecting parameters passed in each operation before the call is executed and after the response is returned.

IParameterInspector’s interface contains the following methods:

public interface IParameterInspector 
{
    void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState);
    object BeforeCall(string operationName, object[] inputs);
}

The ParameterInspector can be inserted both on the client and the server side in the following way:

    /// <summary>
    /// A class defining the parameters inspection behavior
    /// </summary>
    internal class ParameterInspectorBehavior : IOperationBehavior
    {
        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
            
        }

        /// <summary>
        /// Apply MyClientParameterInspector on the client side
        /// </summary>
        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
            clientOperation.ParameterInspectors.Add(new MyClientParameterInspector());
        }

        /// <summary>
        /// Apply MyServerParameterInspector on the client side
        /// </summary>
        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.ParameterInspectors.Add(new MyServerParameterInspector());
            
        }

        public void Validate(OperationDescription operationDescription)
        {
            
        }
    }

IParameterInspector sample

In the following sample MyClientParameterInspector write traces with the parameters sent to each operation, and with the return value.

    internal class MyClientParameterInspector : IParameterInspector
    {
        #region IParameterInspector Members

        public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
        {
            Console.WriteLine(GetCallOutputParameters(operationName, returnValue));
        }

        public object BeforeCall(string operationName, object[] inputs)
        {
            Console.WriteLine(GetCallInputParameters(operationName, inputs));
            return null;
        }

        private static string GetCallOutputParameters(String operationName, Object returnValue)
        {
            return String.Format
                ("Output for operation '{0}': '{1}'",
                 operationName,
                 (returnValue == null ? "Null" : returnValue.ToString()));
        }

        private static string GetCallInputParameters(string operationName, Object[] objects)
        {
            String[] inputs = new string[objects.Length];
            for (int i = 0; i < objects.Length; i++)
            {
                inputs[i] = (objects[i] == null ? "Null" : objects[i].ToString());
            }
            return String.Format
                ("Input for operation '{0}': '{1}'",
                operationName,
                String.Join(", ", inputs));
        }

        #endregion
    }

Advertisements
Posted in: WCF Extensions