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.
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
}
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:
Create a custom attribute that implements IServiceBehavior and use it to mark service classes that are to be modified. When a ServiceHost object is constructed, uses reflection to discover the attributes on the service type. If any attributes implement IServiceBehavior, they are added to the behaviors collection on ServiceDescription.
Instead of implementing IOperationBehaviour
, add required behaviour in the IServiceBehaviour
by looping through all operations:
foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
{
epDisp.DispatchRuntime.MessageInspectors.Add(this);
foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
{
op.ParameterInspectors.Add(this); // JUST AS AN EXAMPLE
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With