单进程资源共享
在开发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.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;
}
}
}
}
}
1 |
测试代码如下
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;
}
}
}
}
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来读写内容了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?