简体   繁体   中英

C# web service - static member variable is becoming empty after 30 minutes of idle time

we have implemented C# web service and deployed in IIS 6.0 server

As per the requirement I need to create a session and hold the same session indefinitely on the application domain until we restart the service.

to implement this I have created a static member variable in the class to hold the session and creating the session on first invocation. This means that the session will be held across all instances in the application scope. The following is the code snippet

public Class MyClass{

 private static ESession session = null;

 public void testMethod(string configlocation){

  if (session == null)
  {
   ISessionConfigurer sessionConfigurer = new ESessionConfigurer();
   sessionConfigurer.ConfigLocation = configLocation;

   session = sessionConfigurer.CreateSession() as ESession;
  }
 }
}

Here I can't instantiate the session object on the declaration itself as it requires parameter 'configLocation' to be passed from other class.

what I observed was that the session object is available for multiple invocations across all instances but after after 30 minutes of idle time the session object is setting to null again.

My initial thought was that this is happening due to the IIS server recycling the objects. Is it correct? if yes how to prevent recycling the objects forever? what configuration parameters I need to set on the IIS server 6.0 application pool

Is there any implications if we set application pool not to recycle like memory issues etc. or is there any better way to implement my requirement.

"hold the same session indefinitely" is not the correct solution to this problem, neither is fiddling with IIS to prevent the AppDomain from unloading.

A better, long term solution which doesn't require any funky configuration is to persist your session to a file, or database, or somewhere else before the application terminates. When the application starts back up, you can reconstruct your persisted session (if it exists), or create a new one.

Easiest way to approach this is to make use of the global.asax file at the root of your application (if it doesn't exist, Visual Studio can create one for you, or you can find plenty of samples online to create it yourself), then override the following methods:

void Application_Start(object sender, EventArgs e)
{
    // initialize your session
}

void Application_End(object sender, EventArgs e)
{
    // persist your session
}

为什么不只使用ASP.NET SQL Server会话状态

C# Web Service acts like a C# Asp Web Site. IIS unloads it from memory after certain (configurable) time. When user connects after that it gets compiled and run again with default variables.

Add pinger, change IIS times or convert it to WCF so You can host it in Your own WindowsService class instance. I prefer the last solution because You get control and it's not a workaround but best practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM