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?
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);
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With