C#: Microsoft Enterprise Library: Policy Injection

Category: .NET Framework

C#: Microsoft Enterprise Library: Policy Injection

By: Zack Turnbow

Introduction

In this last article covering Microsoft’s Enterprise Library, the Policy Injection Application Block (PIAB), will be covered. The PIAB was not one of the original application blocks and did not appear until the release of Enterprise Library 3.0. The PIAB is probably one of the least used application blocks. This may be due to the fact that most developers are unaware what the PIAB has to offer. This article will demonstrate some of the highlights of the PIAB.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Implementation

As shown in the previous articles, the Enterprise Library helps solve the major dilemmas across the layers of applications. The PIAB assists in combining the other application blocks so that they can be applied to the application and by giving the ability to configure each policy without having to rely on any code. The PIAB is often used in conjunction with the following application blocks: Caching, Exception Handling, Logging, and Validation.

To demonstrate the PIAB, a clean slate is needed so the previous projects from the other articles will not be used. So, open Visual Studio and create a console application project. Since the PIAB is going to be used, references to those dlls need to be added to the project. Go ahead and add references to Enterprise Library Shared Library, Enterprise Library Policy Injection Application Block, and the Enterprise Library Handlers for Policy Injection Application Block.


[Click to see full-size]

Next, add the following using statements so that the references can be used:

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Now enter in the following code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;

namespace PIAB_Demo
{
class Program
{
static void Main(string[] args)
{
// Originally:
// MyClass mc = new MyClass();
MyClass mc = PolicyInjection.Create();

mc.TestMe();
}
}

// Originally:
// class MyClass
class MyClass : MarshalByRefObject
{
public void TestMe()
{
Console.WriteLine("The test passed");
}
}
}

As seen in the above code, the normal way of creating the class and using the class are a bit different. First, for the policy injection to work it needs the ability to be captured by the PIAB and for the PIAB to do this the class must be obtained from a MarshalByRefObject. Secondly, when a new instance of the class is created, it must be done by using the Policy Injection create method.

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The following steps will be used to configure the injection policy. So create an app.config file in the project.


[Click to see full-size]

As before, open the app.config file in the Enterprise Library Configuration application. Right click on the app.config file in the left pane and select New – Policy Injection Application Block.


[Click to see full-size]

Next, right click the Policies node and select New – Policy and in the right pane give it a name of TestPolicy.


[Click to see full-size]

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

From there, right click on the Matching Rules node and select New – Type Matching Rule. This will give the ability to catch the method calls.


[Click to see full-size]

In the right pane configure the Matches collection by giving it the value of the class, PAIB_Demo.MyClass.


[Click to see full-size]

Once this is completed, a handler needs to be configured, so right click the Handlers node in the left pane and select New – Logging Handler. In the right pane set the AfterMessage to Ends, the BeforeMessage to Starts, and LogBehavior to BeforeAndAfter.


[Click to see full-size]

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Use the skills learned in the previous article on the logging application block to add logging to the app.config. Then run the application and take a look at the log file created to see the results.


[Click to see full-size]

As shown above, the PIAB is extremely customizable and can be configure without changing any code.

What have we learned?

How to configure polices in the Enterprise Library Configuration application.
How to apply a policy to a type instance without changing any code.
How to build a custom handler and associating the appropriate policy to the handler.

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!