Configuration for Custom Matching Rules and Handlers

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The built-in matching rule and handler classes provided with the Policy Injection Application Block have appropriate design support classes for the Configuration Console and Visual Studio Configuration Editor, which define how they appear in the configuration tools and how users can edit their configurable parameters. You can create similar design classes for your own matching rule and handler classes, if required.

However, if you do not add design support to your custom matching rules and handlers, users can still configure them using the Enterprise Library configuration tools. If your custom class implements the appropriate interface and carries the appropriate configuration attribute, it will appear in the list of available classes in the Configuration Console and the Visual Studio Configuration Editor. For a custom matching rule, your class must implement the IMatchingRule interface, and carry the attribute [ConfigurationElementType(typeof(CustomMatchingRuleData))]. For a custom handler, your class must implement the ICallHandler interface, and carry the attribute [ConfigurationElementType(typeof(CustomCallHandlerData))].

When you follow this approach, the Enterprise Library configuration tools will allow users to enter a series of name/value pairs for the class and will store these as additional attributes of the <add> element for that class. At run time, the configuration system will automatically generate and pass to the constructor of your class a NameValueCollectioncontaining all of the attributes found in the configuration and their values.

Therefore, in the constructor of a custom matching rule or handler class, you can extract from the NameValueCollection and save locally any parameter values that you require. For example, if the <add> element that adds your custom matching rule to the application block contains the attribute UserName="some-user", your class would extract the attribute value, as shown in the following code example.

[ConfigurationElementType(typeof(CustomMatchingRuleData))]public class MyCustomMatchingRule : IMatchingRule
{
  private string userName;

  public MyCustomMatchingRule (NameValueCollection configValues)
  {
    this.userName = configValues["UserName"];
  }

  public bool Matches(MethodBase method)
  {
    //... code that uses the user name and returns true or false ...
  }
}
<ConfigurationElementType(GetType(CustomMatchingRuleData))> _Public Class MyCustomMatchingRule : Implements IMatchingRule

  Dim username As String

  Public Sub New(ByVal configValues As NameValueCollection)
     Me.userName = configValues("UserName")
  End Sub

  Public Function Matches(ByVal method As MethodBase) As Boolean _
       Implements IMatchingRule.Matches
    ' ... code that uses the user name and returns true or false ...
  End Function
End Class

For details about how users configure a custom matching rule, see Configuring Matching Rules. For details about how users configure a custom handler, see Configuring Pipeline Handlers.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.