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.
According to IServiceBehaviour documentation, if you implement this interface and create an attribute and put it at the class level, it will be applied to all operations:
UPDATE
Instead of implementing
IOperationBehaviour, add required behaviour in theIServiceBehaviourby looping through all operations: