什么是静态字典的生命周期 |生活方式瞬态 | C# | WCF
what is lifetime of static dictionary | LifestyleTransient | C# | WCF
我将一些信息存储在 WCF 服务组件内的 class 中定义的静态字典中,如下所示:
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 应用程序中使用了 castle windsor 依赖注入,并且 UserAuthenticator
class 像下面这样向 LifestyleTransient()
注册
Component.For<IUserAuthentication, UserAuthenticator>().LifestyleTransient()
请教我最好的解决方案
提前致谢
终于解决了上面的问题
因为我在 WCF
项目中使用了静态 ConcurrentDictionary
并且还在每个进程中实现了 web 花园和静态变量,所以它在某些时候无法在另一个进程中使用 web gardern
解决方案现在已经停止了 wen garden,它工作正常,将来会用 web garden 实现分布式缓存,如(Radis、NCache 等)
感谢@mjwills 和@Shantanu 的宝贵意见
我将一些信息存储在 WCF 服务组件内的 class 中定义的静态字典中,如下所示:
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 应用程序中使用了 castle windsor 依赖注入,并且 UserAuthenticator
class 像下面这样向 LifestyleTransient()
注册
Component.For<IUserAuthentication, UserAuthenticator>().LifestyleTransient()
请教我最好的解决方案
提前致谢
终于解决了上面的问题
因为我在 WCF
项目中使用了静态 ConcurrentDictionary
并且还在每个进程中实现了 web 花园和静态变量,所以它在某些时候无法在另一个进程中使用 web gardern
解决方案现在已经停止了 wen garden,它工作正常,将来会用 web garden 实现分布式缓存,如(Radis、NCache 等)
感谢@mjwills 和@Shantanu 的宝贵意见