微软企业库5.0学习笔记(10)ASP.NET模块依赖注入
	
    
    
	    您可以使用HTTP模块,一个到ASP.NET HttpApplicationState类的扩展,在Global.asax编写代码强制ASP.NET在每一个页面请求时自动注入依赖的对象,就像在ASP.NET Web窗体应用程序中讨论的一样.
    下列方法显示了一个合适的方法能够获取PreRequestHandlerExecute事件将它自己注入到ASP.NET的执行流水线,在每个页面请求中通过容器的BuildUp方法运行Http模块,并获取OnPageInitComplete事件。当OnPageInitComplete执行时模块代码按照所有的控件树运行,并通过容器的BuildUp方法处理每个控件。
    BuildUp方法获取已经存在的对象实例,处理并填充类的依赖,返回实例。如果没有依赖则返回最初的实例。
     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))
      {
        container.BuildUp(c.GetType(), c);
      }
      context.PreRequestHandlerExecute -= OnPreRequestHandlerExecute;
    }
    // Ge
    
     
	
	
    
    
	相关文档:
        
    
    在Global.asax启动一条线程就ok了,下面是启动线程定时写文件的例子 
在Global.asax 
C# code:
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<script runat="server">   
 string LogPath;  ......
	
    
        
    
        protected void Page_Load(object sender, EventArgs e)
    {
    }
    #region OnPreInit 第一步
    protected override void OnPreInit(EventArgs e)
    {
        //检查 ......
	
    
        
    
    例一:
+++ 第一个页面
++ 页面代码如下:
<div>
 <a href="QueryString04.aspx?blog=DoooooNetooooot.blog.tianya.cn&qq=516535763">超链接传值示例</a>
</div>
++ 后台代码如下:
(略)
+++ 第二个页面
++ 页面代码如下:
(略)
++ 后台代码如下:
protected void Page_Load( ......
	
    
        
    
    +++ 页面代码如下:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
+++ 后台代码如下:
protected void Butto ......
	
    
        
    
    例一:
+++ 修改Global.asax文件:
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e) 
{  }
void Application_End(object sender, EventArgs e) 
{  }
void Application_Error(object sender, EventArgs e) 
{  }
void ......