【发布时间】:2019-06-07 12:05:01
【问题描述】:
我将一些信息存储在静态字典中,该字典在 WCF 服务组件内部的类中定义,如下所示:
public class UserAuthenticator : IUserAuthentication
{
public static ConcurrentDictionary<UserInfo, ConcurrentDictionary<string, BookingDetails>>BookingDetailsDictionary = new ConcurrentDictionary<UserInfo, ConcurrentDictionary<string, BookingDetails>>(new UserEqualityComparer());
public static ConcurrentDictionary<string, Connector> connectorDictionary = new ConcurrentDictionary<string, Connector>();
public BookingDetails Authenticate(UserInfo userInfo, ServiceDetails serviceDetail, XmlElement requestData)
{
var bookDetails = new BookingDetails();
try
{
ConcurrentDictionary<string, BookingDetails> dicObject = null;
if (bookingDictionary.TryGetValue(userInfo, out dicObject))
{...}
else
{
// call Database and get value from database and fill db value in to static ConcurrentDictionary
}
}
}
}
这里我检查静态 ConcurrentDictionary 键,如果值不在字典中,然后调用数据库并在字典中填充值。
预期的输出是第一次调用 wcf 服务然后调用数据库并在 ConcurrentDictionary 中填充值,然后在所有 WCF 服务调用之后从 ConcurrentDictionary 读取数据
现在,问题是有时我看到静态 ConcurrentDictionary 计数为零。奇怪的是应用程序池仍然处于活动状态。没有应用程序池被回收,它仍然随机调用数据库,有时它从 ConcurrentDictionary 获取数据
这对我来说真的很奇怪。我假设静态变量将保持其值,直到应用程序结束。但即使应用程序池没有回收或 IIS 没有重新启动,静态变量也会归零。
你有什么建议?使用 ConcurrentDictionary 变量是更好的选择吗?
注意:我在我的 wcf 应用程序中使用了城堡温莎依赖注入,UserAuthenticator 类在LifestyleTransient() 注册,如下所示
Component.For<IUserAuthentication, UserAuthenticator>().LifestyleTransient()
请给我最好的解决方案
提前致谢
【问题讨论】:
-
您的应用程序可以扩展吗?如果是,那么这可能是原因
-
您在使用网络花园吗? forums.iis.net/t/1237492.aspx
-
@mjwills:是的,我正在使用网络花园
-
这就是原因。
static是每个process。 -
@mjwills:所以你的意思是我需要删除网络花园?
标签: c# wcf static castle-windsor concurrentdictionary