你可以寫這可能在web.config中註冊一個HttpModule:
public class MyModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.EndRequest += Application_EndRequest;
}
private void Application_EndRequest(Object source, EventArgs e)
{
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}
public void Dispose()
{
}
}
,並在web.config中:
<httpModules>
<add name="MyModule" type="AppName.MyModule, AppName" />
</httpModules>
另一個更MVCish方式是寫一個全球性的動作過濾器:
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}
}
可以在註冊的方法:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new MyActionFilterAttribute());
}
您也可以註冊您的模塊,而無需編輯的web.config:http://blog.davidebbo.com/2011/02/register-your-http-模塊-AT-runtime.html – 2011-03-11 17:58:15