1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Collections;
 6 using System.Data.Linq;
 7 
 8 /// <summary>
 9 /// 实现自动抛弃当前数据库上下文的模块。
10 /// </summary>
11 public sealed class DataContextAutoDisposeModule : IHttpModule
12 {
13     #region IHttpModule 成员
14     public void Init(HttpApplication context)
15     {
16         context.PostRequestHandlerExecute += new EventHandler(Enter);
17     }
18 
19     public void Dispose()
20     {
21     }
22     #endregion
23 
24     private void Enter(object sender, EventArgs e)
25     {
26         IDictionary httpContextItems = HttpContext.Current.Items;
27         List<object> keys = new List<object>(httpContextItems.Count);
28 
29         foreach (DictionaryEntry item in httpContextItems)
30         {
31             DataContext dataContext = item.Value as DataContext;
32             if (dataContext != null)
33             {
34                 dataContext.Dispose();
35                 keys.Add(item.Key);
36             }
37         }
38 
39         foreach (object key in keys)
40         {
41             httpContextItems.Remove(key);
42         }
43     }
44 }

相关文章:

  • 2022-12-23
  • 2022-03-07
  • 2021-12-02
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-30
  • 2022-01-18
  • 2022-02-25
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案