Reputation: 10108
I have a WCF service running on IIS.
Every time a user logs in (with username and password) I create a SessionClass that does something every few minutes in a thread and handle user requests.
I save every new SessionClass that's created in a Dictionary in a singleton ServiceClass.
If a user doesn't request anything for a long period of time (let's say 10 hours) the session should die. I kill the thread and remove the SessionClass from the Dictionary in the ServiceClass singleton.
My problem is that every time I remove a SessionClass from the Dictionary, it makes the instance of the ServiceClass null again and the next time its being called, it's being created again so I lose all the sessions I used to hold in the dictionary...
I tried holding direct reference inside the WCF service to the ServiceClass.GetInstance() (I thought that the garbage collector is killing the ServiceClass) but it won't help...
Why is this happening? Any ideas?
Upvotes: 2
Views: 984
Reputation: 41757
Declare your Dictionary as a static field.
As to why this is currently happening it's impossible to say without more information, please post some code.
Update
Based on your comment it sounds like IIS is recyling the AppPool and / or AppDomain. In either event your AppDomain is unloaded, so all information stored in memory will be lost.
Tess Ferrandez wrote a very good blog post about why IIS does this here
Update 2
If it's merely inconvenient that the data gets lost you may try hosting your service as a managed windows service instead. The scenario enabled by the managed Windows Service hosting option is that of a long-running WCF service hosted outside of IIS in a secure environment that is not message-activated. The lifetime of the service is controlled instead by the operating system. If, however, it is critical that your data is never lost, you'll need to add in a persistence mechanism (database, store in a file on disk etc) and read from / write to there.
Upvotes: 1