2

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?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
YogevSitton
  • 10,068
  • 11
  • 62
  • 95

1 Answers1

1

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.

Community
  • 1
  • 1
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • I declared the dictionary as static but it still didn't help. Here's the class: `public class ServiceClass { private static ServiceManager _instance; private static Dictionary _sessions = new Dictionary(); private ServiceClass() { } public static ServiceClass GetInstance() { return _instance ?? (_instance = new ServiceClass()); } }` – YogevSitton Nov 14 '11 at 14:24
  • hemmm... sounds like the reason. I changed the recycling settings on the IIS for the AppPool I'm using. Is there any way to make sure the AppPool will never recycle? If not - what are my options? How can I make sure not to lose information? – YogevSitton Nov 14 '11 at 15:13