Accession to the wcf pipeline

Is there any way to check the wcf method call. I mean everything that matches the HttpModule in asp.net.

I want to execute a method before calling the method.

+5
source share
3 answers

WCF has a very extensible architecture. You can intercept and configure many WCF functions to do your own work.

In your case, you will need to write the appropriate service or endpoint behavior. The process between receiving a message from transport channels and calling your service method is called scheduling. WCF uses a set of behaviors to perform dispatch. You can provide your own behavior for checking method calls.

- http://msdn.microsoft.com/en-us/magazine/cc163302.aspx

WCF http://msdn.microsoft.com/en-us/library/aa480210.aspx

+7

, . - , Custom Encoder , , , Custom Message Inspector.

.

0

IOperationInvoker, , , :

public class MyOperationInvoker : IOperationInvoker
{
    IOperationInvoker originalInvoker;
    public MyOperationInvoker(IOperationInvoker originalInvoker)
    {
        this.originalInvoker = originalInvoker;
    }

    public bool IsSynchronous { get { return originalInvoker.IsSynchronous; } }

    public object[] AllocateInputs() { return originalInvoker.AllocateInputs(); }

    public object Invoke(object instance, object[] inputs, out object[] outputs)
    {
        //Do stuff before call
        var res = this.originalInvoker.Invoke(instance, inputs, out outputs);
        //stuff after call
        return res;
    }

    public IAsyncResult InvokeBegin(object instance, object[] inputs,
                AsyncCallback callback, object state)
    {
        //Do stuff before async call
        var res = this.originalInvoker.InvokeBegin(instance, inputs, callback, state);
        return res;
    }

    public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
    {
        var res = this.InvokeEnd(instance, out outputs, result);
        //Do stuff after async call
        return res;
    }
}

:

public class MyBehaviorAttribute : Attribute, IServiceBehavior, IOperationBehavior
{
    //IOperationBehavior
    public void ApplyDispatchBehavior(OperationDescription operationDescription,
            DispatchOperation dispatchOperation)
    {
        dispatchOperation.Invoker = new MyOperationInvoker(dispatchOperation.Invoker);
    }

    public void AddBindingParameters(OperationDescription operationDescription,
            BindingParameterCollection bindingParameters) { /*Do nothing*/ }

    public void ApplyClientBehavior(OperationDescription operationDescription,
            ClientOperation clientOperation) { /*Do nothing*/ }

    public void Validate(OperationDescription operationDescription) { /*Do nothing*/ }

    //IServiceBehavior
    public void Validate(ServiceDescription serviceDescription,
                ServiceHostBase serviceHostBase) { /*Do nothing*/ }

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

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
                ServiceHostBase serviceHostBase)
    {
        foreach (ServiceEndpoint endpoint in serviceHostBase.Description.Endpoints)
        {
            foreach (var operation in endpoint.Contract.Operations)
            {
                operation.Behaviors.Add(this);
            }
        }
    }

:

[MyBehavior]
public class HelloService : IHelloService
{
    ...
}
0

All Articles