【问题标题】:what is lifetime of static dictionary | LifestyleTransient | C# | WCF什么是静态字典的生命周期 |生活方式瞬态 | C# | WCF
【发布时间】: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


【解决方案1】:

终于解决了上面的问题

由于我在WCF 项目中使用了静态ConcurrentDictionary,并且还为每个进程实现了网络花园和静态变量,因此它在网络花园的另一个进程中有时无法工作

解决方案现在停止了文园,它工作正常,未来将通过网络园实现分布式缓存,如(Radis、NCache 等)

感谢 @mjwills 和 @Shantanu 提供宝贵的 cmets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多