I followed the steps in the Microsoft Enterprise Library 5.0 documentation to create an HTTP module for entering a link to the Enterprise Library container on the pages of an ASP.NET web application.
It contains the following code (which also appears on the Internet here ):
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Microsoft.Practices.Unity;
namespace Unity.Web
{
public class UnityHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
public void Dispose() { }
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
IHttpHandler currentHandler = HttpContext.Current.Handler;
HttpContext.Current.Application.GetContainer().BuildUp(
currentHandler.GetType(), currentHandler);
var currentPage = HttpContext.Current.Handler as Page;
if (currentPage != null)
{
currentPage.InitComplete += OnPageInitComplete;
}
}
private void OnPageInitComplete(object sender, EventArgs e)
{
var currentPage = (Page)sender;
IUnityContainer container = HttpContext.Current.Application.GetContainer();
foreach (Control c in GetControlTree(currentPage))
{
container.BuildUp(c.GetType(), c);
}
context.PreRequestHandlerExecute -= OnPreRequestHandlerExecute;
}
private IEnumerable<Control> GetControlTree(Control root)
{
foreach (Control child in root.Controls)
{
yield return child;
foreach (Control c in GetControlTree(child))
{
yield return c;
}
}
}
}
}
There are a number of problems with this code and the instructions that came with it.
1) The instructions do not mention where to place this code. Since this is a class, I put it in the App_Code folder of my ASP.NET website project.
In fact, here are the instructions for this bit of code:
HTTP ASP.NET(, , UnityHttpModule) , PreRequestHandlerExecute , , Unity BuildUp .
2) HttpContext.Current.Application. GetContainer() , DLL- ( .NET 4.0).
3) OnPageInitComplete "context"... , , .
, ?