单进程资源共享

在开发web时我们可以用HttpContext.Current.Items来共享单个进程贯穿整个生命周期的资源

 如:为每一WEB请求使用某一个Conection(这样做的好处是不要每次用数据库就打开,关闭,可以提高性能,可以最后通过HttpModule来关闭)

 

在开发CS程序的时候,或者写驱动测试WEB的时候HttpContext.Current.Items会报错,最近开发一个框架,有幸遇到了这个问题,我是这样解决的(当然其解决方法也是看了框架的代码后才写出来的)。

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Security.Permissions;
using System.Runtime.Remoting.Messaging;
using System.Runtime.InteropServices;
using System.Collections;

namespace Fox.Tools.ThreadTool
{
  [Serializable, ComVisible(
true), SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
   
public class ThreadResourse
    {
      
private IDictionary _items;
      
public IDictionary Items
      {
          
get
          {
              
if (this._items == null)
              {
                  
this._items = new Hashtable();
              }
              
return this._items;
          }
      }

      
public static ThreadResourse Current
      {
          
get
          {
              
return (ContextBase.Current as ThreadResourse);
          }
          
set
          {
              ContextBase.Current 
= value;
          }
      }

       
internal class ContextBase
       {
           
// Methods
           internal static object SwitchContext(object newContext)
           {
               
object hostContext = CallContext.HostContext;
               
if (hostContext != newContext)
               {
                   CallContext.HostContext 
= newContext;
               }
               
return hostContext;
           }


           
// Properties
           internal static object Current
           {
               
get
               {
                   
if (null == CallContext.HostContext)
                   {
                       CallContext.HostContext 
= new ThreadResourse();
                   }
                   
return CallContext.HostContext;
               }
               [SecurityPermission(SecurityAction.Demand, Unrestricted 
= true)]
               
set
               {
                   CallContext.HostContext 
= value;
               }
           }
       }



    }
}

测试代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Fox.Tools.ThreadTool;
namespace testlibrary
{
    [TestFixture]
   
public class Class4
    {
        [Test]
        
public void testjiagou()
        {
           
// Category me = BasicDAL<Category>.GetObject(5);
            ThreadResourse.Current.Items.Add("key""value");
            
string ss = ThreadResourse.Current.Items["key"as string;
            
        }
    }
}

如期的达到了效果。

在开发的底层框架内部,我这样设计

 
public partial class GlobalInfo
    {
        
public static IDictionary Items
        {
            
get
            {
                
if (GlobalInfo.IsWebSite) //如果是网站开发则返回HttpContext.Current.Items              

               {
                    
return HttpContext.Current.Items;
                }
                
else
                {
                    
return ThreadResourse.Current.Items;
                }
            }
        }

这样,外界就只需要调用GlobalInfo.Items来读写内容了。

原文地址:https://www.cnblogs.com/Creator/p/threadresourse.html