Microsoft enterprise database 5.0 Study Notes (10) ASP. NET module dependency Injection

Source: Internet
Author: User

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/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.