Enterprise Library Call Handlers
The patterns & practices Enterprise Library includes a set of call handlers and the equivalent call handler attributes designed for use with the Unity PolicyInjectionBehavior. This section discusses some of the common scenarios for using the Policy Injection Application Block call handlers. It examines the following common scenarios where the block can simplify and accelerate application development:
- Logging Method Invocation and Property Access
- Handling Exceptions in a Structured Manner
- Validating Parameter Values
- Authorizing Method and Property Requests
- Measuring Target Method Performance
Logging Method Invocation and Property Access
Enterprise applications usually provide rich and detailed information about their operation in order to help administrators and operators measure activity, audit actions that take place, and detect unusual behavior, errors, and failures. Enterprise Library provides a handler that takes advantage of the features exposed by the Enterprise Library Logging Application Block. These features allow developers and administrators to configure logging to the Windows Event Log, e-mail messages, databases, message queue systems, text files, Windows Management Instrumentation (WMI) events, or custom locations. For more details about logging method invocations and property accesses, see The Logging Handler.
Handling Exceptions in a Structured Manner
All applications should handle errors in order to protect the application and to provide information for users, operators, administrators, and developers. Within .NET Framework-based applications, the most common scenario is handling exceptions raised by the application code, executing suitable recovery tasks where practicable, and raising the exception to the calling code or user interface.
Enterprise Library provides a handler that takes advantage of the features exposed by the Enterprise Library Exception Handling Application Block. These features allow developers to create a consistent strategy for processing exceptions that occur in all architectural layers of an enterprise application. They also allow administrators to define and maintain exception handling policies, and they support the following:
- Logging exception information.
- Hiding sensitive information by replacing the original exception with another exception.
- Wrapping exceptions inside a new exception to maintain contextual information.
- Combining these tasks so that, for example, the application block will log the information from an exception and then replace the original exception with another.
For more details about structured exception handling using policy injection, see The Exception Handling Handler.
Validating Parameter Values
Applications should protect against errors caused by invalid or out-of-range values provided as input to the user interface or applied to methods or properties within the code. Writing validation code is always an onerous task and there is always the chance that you will repeat an action or overlook some unexpected situation. Enterprise Library provides a handler that takes advantage of the features exposed by the Validation Application Block; this minimizes the code developers have to create and it allows administrators to modify the validation policies through configuration if required.
For more details about applying validation to class members using policy injection, see The Validation Handler.
Authorizing Method and Property Requests
Distributed applications frequently use services and access classes that reside in separate security contexts. If the current user does not have permission to access a target class instance, an exception occurs that may be difficult to diagnose. Administrators and operators often find that a large proportion of application installation, set up, and run-time maintenance involves setting appropriate permissions and authorizing users and services.
Enterprise Library provides a handler that will check the authorization status of the requesting user or thread against the target class permissions before calling the target method or accessing the target property. The authorization handler takes advantage of the features exposed by the Enterprise Library Security Application Block, using an authorization provider defined in the application configuration.
For more details about authorizing requests to members of a class using policy injection, see The Authorization Handler.
Measuring Target Method Performance
Enterprise Library contains instrumentation that allows developers, administrators, and operators to monitor the performance of the application blocks they use in their applications. In most cases, developers will also include instrumentation, such as performance counters, within their code to support monitoring of classes and resources used by the application. Enterprise Library and Unity provide additional and easy-to-use features for monitoring performance of target classes in the form of a handler that measures the time taken for the request to pass through the handler pipeline to the target class instance, and back to the application.
One such feature is part of the logging handler; it requires only the addition of this handler as the first in the policy pipeline. The logging handler takes advantage of the features exposed by the Enterprise Library Logging Application Block, and it exposes the call duration as a property of the TraceLogEntry class. For more details about how the CallTime property exposed by the logging handler can provide performance measurement for individual methods, see The Logging Handler.
Another option for measuring the performance of the target methods is to use the performance counters handler. This handler increments a number of performance counters that provide detailed performance information, including the average execution time for the method and the number of times the method is called. For more information, see The Performance Counter Handler.
To understand how call handlers and the interception mechanism work, and how you can configure interception and policy injection, see the following topics :