Using IParameterInspector.BeforeCall(string operationName, object[] inputs) to abort a call

蓝咒 提交于 2019-12-10 16:18:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!