Search code examples
c#wcfstaticcastle-windsorconcurrentdictionary

what is lifetime of static dictionary | LifestyleTransient | C# | WCF


I am storing some information in static dictionary which is defined in class inside WCF service component like below :

    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
                    }
                }
            }
    }

Here I check static ConcurrentDictionary key if value in the not in dictionary then call database and fill value in the dictionary.

Expected output is first time invoke wcf service then call database and fill value in the ConcurrentDictionary and then after all the WCF service call read data from ConcurrentDictionary

Now, problems is sometimes I see that the static ConcurrentDictionary count are zeroed. And the strange part is the application pool is still active. no application pool is recycle still randomly it call database and sometime it take data from ConcurrentDictionary

This is really strange for me. I assume that static variable will hold its value until the application ends. But even the application pool did not recycle or IIS is not restarted, the static variable is zeroed.

What do you suggest? Is using ConcurrentDictionary variables a better choice?

Note : I have used castle windsor dependency injection in my wcf application and UserAuthenticator class is register with LifestyleTransient() like below

Component.For<IUserAuthentication, UserAuthenticator>().LifestyleTransient()

Please advice me the best solution

Thanks in advance


Solution

  • Finally I got solution of above problem

    As I have used static ConcurrentDictionary in WCF project and also implemented web garden and static variables per process so its not working in another process some time with web gardern

    Solution is as off now stopped wen garden and its working fine and in future will implement distributed cache like (Radis, NCache, etc) with web garden

    Thanks to @mjwills and @ Shantanu for valuable comments