WCF ChannelFactory和信道的高速缓存在ASP.NET 客户应用程序
-
22-07-2019 - |
题
我建设的一系列WCF服务,将要使用多于一个应用程序。因为我是试图限定一个共用图书馆的访问WCF服务。
知道每个服务请求,由不同用户应该使用不同通道的我在思考在高速缓冲通道每请求(HttpContext.Current.Items
)和缓ChannelFactory用于创建的道每个应用程序(HttpApplication.Items
)因为我可以创建多个频道的一样 ChannelFactory
.
然而,我有一个问题,关于这个高速缓存机构,当它涉及到关闭ChannelFactory和通道。
- 我需要关闭通道后,用,在结束的请求,或者是否把它留在那里被关闭(?) 在该上下文中,请求的死亡?
- 什么约ChannelFactory?由于每个信道的是相关联的ChannelFactory,创造了它,它是安全的保持相同ChannelFactory在生活中的应用程序(应用程序域)?
这是代码,我在使用管理这样的:
public class ServiceFactory
{
private static Dictionary<string, object> ListOfOpenedChannels
{
get
{
if (null == HttpContext.Current.Items[HttpContext.Current.Session.SessionID + "_ListOfOpenedChannels"])
{
HttpContext.Current.Items[HttpContext.Current.Session.SessionID + "_ListOfOpenedChannels"] = new Dictionary<string, object>();
}
return (Dictionary<string, object>)HttpContext.Current.Items[HttpContext.Current.Session.SessionID + "_ListOfOpenedChannels"];
}
set
{
HttpContext.Current.Items[HttpContext.Current.Session.SessionID + "_ListOfOpenedChannels"] = value;
}
}
public static T CreateServiceChannel<T>()
{
string key = typeof(T).Name;
if (ListOfOpenedChannels.ContainsKey(key))
{
return (T)ListOfOpenedChannels[key];
}
else
{
ChannelFactory<T> channelF = new ChannelFactory<T>("IUsuarioService");
T channel = channelF.CreateChannel();
ListOfOpenedChannels.Add(key, channel);
return channel;
}
}
}
谢谢!
解决方案
地区关闭通道,尽快为你做它。这将把它回到道的游泳池,因此它可以采用由另一个工作人员的螺纹。
是的,通道的工厂(在昂贵的位)可持续的使用寿命应用程序。
更新
为。净额4.5有一个建立在缓存选择的工厂 ChannelFactory缓存。净额4.5
其他提示
这是一个在一边。你为什么使用。作为一个上下文的钥匙吗?上下文。项目是独特的每一请求。这是:
HttpContext.Current.Items[HttpContext.Current.Session.SessionID +"_ListOfOpenedChannels"]
应该在功能上相当于:
HttpContext.Current.Items["ListOfOpenedChannels"]
不隶属于 StackOverflow