Creating Interception Handler Attributes

Handler attributes allow developers to apply handlers to classes and class members directly, without configuring them in the application configuration file. Developers creating custom handlers may want to provide an attribute for their handlers. To build a custom handler attribute, you create a class that derives from the HandlerAttribute base class shown here.

public abstract class HandlerAttribute : Attribute
{
  /// Derived classes implement this method. When called, it creates a 
  /// new call handler as specified in the attribute configuration.
  /// The parameter "container" specifies the IUnityContainer 
  /// to use when creating handlers, if necessary.
  /// returns a new call handler object.
  public abstract ICallHandler CreateHandler(IUnityContainer container);

  private int executionorder;
  /// <summary>
  /// Gets or sets the order in which the handler will be executed.
  /// </summary>
  public int Order
  {
    get { return this.executionorder; }
    set { this.order = value; }
  }
}

In your custom attribute class, you must implement one or more constructors that accept values from the attribute, and/or implement named properties that the developer can use to set the properties of the class. Then you simply override the CreateHandler abstract method declared within the base class to create and return the required handler class as an ICallHandler instance.

Example Call Handler Attribute

As an example, you could create a call handler attribute for a call handler similar to that described in the topic Creating Interception Policy Injection Call Handlers that prevents invocation of business processes on weekend days. In this case, assume that the handler has a property named SaturdayOK that allows you to set it to allow calls to occur on a Saturday. The call handler has two constructors: one that takes a parameter that sets the value of the SaturdayOK property to the specified value (true or false), and one that takes no parameters and sets the default value (false) for the SaturdayOK property. The following code shows an implementation of the WeekdayOnlyCallHandlerAttribute.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)]
public class WeekdayOnlyCallHandlerAttribute : HandlerAttribute
{
  private bool allowSaturday;

  public WeekdayOnlyCallHandlerAttribute()
  {
    allowSaturday = false;
  }

  public WeekdayOnlyCallHandlerAttribute(bool SaturdayOK)
  {
    allowSaturday = SaturdayOK;
  }

  public override ICallHandler CreateHandler(IUnityContainer ignored)
  {
    return new WeekdayOnlyCallHandler(allowSaturday, Order);
  }
}

Notice the AttributeUsage attribute that specifies where developers can apply the new custom attribute (on a class, a property, or a method), and—in this case—the provision of two constructors. The first (default) constructor uses the default value (false), while the second accepts a value for the SaturdayOK property. The CreateHandler method override instantiates the WeekdayOnlyCallHandler class with the appropriate values and returns this as an ICallHandler reference.


Show: