Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Same IParameterInspector for all operations on a service

I have implemented a custom IParameterInspector and I want to have it execute for every single operation on my service.

My understanding is that IParameterInspector implementations can only be used with IOperationBehavior implementations, and that intern IOperationBehavior implementation can only be used to decorate individual operations using an attribute.

Does anyone know if there is a way I can register my IParameterInspector at a service level so that it can execute for all operations in the service?

like image 802
Andy McCluggage Avatar asked Nov 10 '10 11:11

Andy McCluggage


1 Answers

Thanks to this and subsequenbtly this, I found what I was looking for.

IParameterInspector does not need to be at the IOperationBehavior level. They can be at the IServiceBehavior level. In the service level ApplyDispatchBehavior method you need to loop through all its operations and assign the inspector behaviour.

My class in full...

[AttributeUsage(AttributeTargets.Class)]
public class ServiceLevelParameterInspectorAttribute : Attribute, IParameterInspector, IServiceBehavior
{
    public object BeforeCall(string operationName, object[] inputs)
    {
       // Inspect the parameters.
        return null;
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
        {
            if (channelDispatcher == null)
            {
                continue;
            }

            foreach(var endPoint in channelDispatcher.Endpoints)
            {
                if (endPoint == null)
                {
                    continue;
                }

                foreach(var opertaion in endPoint.DispatchRuntime.Operations)
                {
                    opertaion.ParameterInspectors.Add(this);
                }
            }
        }
    }
}
like image 74
Andy McCluggage Avatar answered Nov 16 '22 03:11

Andy McCluggage