wcfservicebehavior

Can I create a ServiceBehavior that will replace the OperationBehaviors I already apply to every Operation in my Service?


I know all the operations in this service will require the same OperationBehavior.. could I implement a ServiceBehavior that will perform the same functionality before the operation is run?

I currently have:

[ServiceContract]
public interface IService
{
    [AuthTokenValidation]
    [OperationContract]
    string DoThis(string authtoken);
}

I want to replace it with:

[AuthTokenValidation]
[ServiceContract]
public interface IService
{
    [OperationContract]
    string DoThis(string authtoken);
}

Inside my AuthTokenValidation attribute, I have the following code in the ApplyDispatchBehavior method:

public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
    AuthTokenInspector inspector;

    inspector = new AuthTokenInspector(AuthTokenIndex);
    dispatchOperation.ParameterInspectors.Add(inspector);
}

I checked the ApplyDispatchBehavior method in IServiceBehavior, but I couldnt figure out how to access the DisppatchOperation that was currently being called.

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
    //Can I reach the OperationDispatch from in here??
}

Is this the proper way to use ServiceBehavior or should I just be satisfied with OperationBehaviors to implement this functionality?


Solution

  • I haven't tried is but I'd have thought you could get to your DispatchOperation as follows: