You can use the HTTP module, one to ASP. net httpapplicationstate class extension, in the global. asax write code to force ASP. net automatically injects dependent objects in each page request, just as in ASP.. NET web forms applications.
The following method shows a suitable method to obtainPrerequesthandlerexecuteEvent to inject itself into the ASP. NET execution pipeline, in each page request through the container'sBuildupMethod To run the HTTP module and obtainOnpageinitcompleteEvent. WhenOnpageinitcompleteWhen executed, the module code runs according to all the control trees and runs throughBuildupMethod to process each widget.
BuildupMethod to obtain an existing object instance, process and fill in class dependencies, and return to the instance. If no dependency exists, the initial instance is returned..
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); // User Controls are ready to be built up after page initialization is complete var currentPage = HttpContext.Current.Handler as Page; if (currentPage != null) { currentPage.InitComplete += OnPageInitComplete; } } // Build up each control in the page's control tree private void OnPageInitComplete(object sender, EventArgs e) { var currentPage = (Page)sender; IUnityContainer container = HttpContext.Current.Application.GetContainer(); foreach (Control c in GetControlTree(currentPage)) { if (!(c is LiteralControl)) container.BuildUp(c.GetType(), c); } context.PreRequestHandlerExecute -= OnPreRequestHandlerExecute; } // Get the controls in the page's control tree excluding the page itself private IEnumerable<Control> GetControlTree(Control root) { foreach (Control child in root.Controls) { yield return child; foreach (Control c in GetControlTree(child)) { yield return c; } } } }}
The following shows the implementation of an Application Status and exposes a staticGetcontainerMethod.
ApplicationStatus to create a new unified container. If it does not exist, or return a reference to an existing instance.
Using system. web; <br/> using Microsoft. practices. unity; </P> <p> namespace unity. web <br/> {<br/> Public static class httpapplicationstateextensions <br/> {<br/> private const string globalcontainerkey = "entlibcontainer "; </P> <p> Public static iunitycontainer getcontainer (this httpapplicationstate appstate) <br/>{< br/> appstate. lock (); <br/> try <br/> {<br/> var mycontainer = appstate [globalcontainerkey] As iunitycontainer; <br/> If (mycontainer = NULL) <br/>{< br/> mycontainer = new unitycontainer (); <br/> appstate [globalcontainerkey] = mycontainer; <br/>}< br/> return mycontainer; <br/>}< br/> finally <br/>{< br/> appstate. unlock (); <br/>}< br/>