Using the Policy Injection Application Block

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 Policy Injection Application Block uses a section within the Enterprise Library configuration to specify its behavior, in conjunction with any directly applied attributes in the target classes. Developers specify the following in the configuration:

  • One or more policies that, together, make up the set of policies for the application block.
  • One or more matching rules for each policy that is not attribute-based. The matching rules for each policy specify the target classes and members for which the Policy Injection Application Block will implement a handler pipeline. A policy is not limited to a single class or class member; depending on the matching rules, it can apply to all the members of one class, all the members of several classes, some members of one class, some members of multiple classes, or some other combination.
  • One or more handlers for each policy. The Policy Injection Application Block will add all the specified handlers to the handler pipeline for this policy. Each handler can be one of the built-in handlers that use features of the other application blocks in Enterprise Library or a custom handler.

The following extract shows an example of the configuration for the Policy Injection Application Block that uses some of the built-in matching rules and handlers provided with the application block.

  <policyInjection>
    <policies>
      <add name="FirstPolicy">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MemberNameMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="MyMemberNameRule1">
            <matches>
              <add match="MyMethodName" ignoreCase="true" />
              <add match="APropertyName" ignoreCase="true" />
            </matches>
          </add>
          <add match="MyTagToFind" ignoreCase="true" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TagAttributeMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="MyTagAttributeRule1" />
        </matchingRules>
        <handlers>
          <add logBehavior="After" beforeMessage="" afterMessage="" eventId="0"
            includeParameterValues="true" includeCallStack="false" includeCallTime="true"
            priority="3" severity="Information" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="MyLoggingHandler1" />
        </handlers>
      </add>
      <add name="AnotherPolicy">
        <matchingRules>
          <add match="MyMethod" ignoreCase="true" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MethodSignatureMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="Method Signature Matching Rule">
            <parameters>
              <parameter name="param1" typeName="System.Int32" />
              <parameter name="param2" typeName="System.String" />
            </parameters>
          </add>
        </matchingRules>
        <handlers>
          <add exceptionPolicyName="CustomerModelPolicy" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="MyExceptionHandler1" />
          <add expirationTime="00:00:30" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.CachingCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="Caching Handler" />
        </handlers>
      </add>
    </policies>
  </policyInjection> 

The Enterprise Library contains tools to help you create and maintain the application configuration file. Figure 1 shows the Visual Studio Configuration Editor displaying the configuration file that contains the preceding extract.

Ff649814.PI-INTRO-Configuration(en-us,PandP.10).gif

Figure 1
The Visual Studio Configuration Editor showing the Policy Injection section

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.
Show: