C# – Apply a WCF attribute to all method in the service

attributesc++wcf

I have a custom attribute that I want to apply to each methods in my WCF service.

I proceed like this:

[MyAttribute]
void MyMethod()
{

}

The problem is that my service contains hundreds of methods and I don't want to write [Attribute] above all of them. Is there a way to apply the attribute to all my methods in my service?

Here's my attribute's signature:

//[AttributeUsage(AttributeTargets.Class)]
public class SendReceiveBehaviorAttribute : Attribute, /*IServiceBehavior,*/ IOperationBehavior

EDIT after Aliostad's answer:

I tried this:

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.Invoker = new OperationInvoker(op.Invoker);
            }
        }
    }
}

And that:

public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
    foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
    {
        foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
        {
            foreach (DispatchOperation op in eDispatcher.DispatchRuntime.Operations)
            {
                op.Invoker = new OperationInvoker(op.Invoker);
            }
        }
    }
}

But it still don't work.

Best Solution

The following should do the trick by using the service behavior to add the correct operation behavior which calls the invoker.

public class MyAttribute : Attribute, IServiceBehavior, IOperationBehavior
{
    #region IServiceBehavior Members
    public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase host)
    {
        foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
        {
            foreach (var operation in endpoint.Contract.Operations)
            {
                operation.Behaviors.Add(this);
            }
        }
    }
    ...
    #endregion

    #region IOperationBehavior Members
    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.Invoker = new OperationInvoker(dispatchOperation.Invoker);
    }
    ...
    #endregion
}