问题
I have a custom behavior in which I implement "IParameterInspector" to be able to use BeforeCall and AfterCall. I'm currently using these methods to make some verifications before the call execute on my WCF service.
Here's my Attribute:
[AttributeUsage(AttributeTargets.Class)]
public class SendReceiveBehaviorAttribute : Attribute, IServiceBehavior
{
public SendReceiveBehaviorAttribute()
{
}
public void ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host)
{
foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers)
{
foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
{
foreach (DispatchOperation op in eDispatcher.DispatchRuntime.Operations)
{
op.ParameterInspectors.Add(MyInspector);
}
}
}
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
And my inspector:
internal class MyInspector: IParameterInspector
{
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
}
public object BeforeCall(string operationName, object[] inputs)
{
// Make some verifications and cancel if it fails.
return null;
}
}
EDIT What would be the best way to abort my call if my verification fails?
回答1:
Throwing exception is the only way - as far as I am aware - to abort.
来源:https://stackoverflow.com/questions/5486563/using-iparameterinspector-beforecallstring-operationname-object-inputs-to-a