简体   繁体   English

C#Web服务-闲置30分钟后,静态成员变量将变为空

[英]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 我们已经实现了C#Web服务并部署在IIS 6.0服务器中

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. 在这里,我无法在声明本身上实例化会话对象,因为它需要从其他类传递参数“ configLocation”。

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. 我观察到的是,该会话对象可用于所有实例的多次调用,但是在闲置30分钟之后,该会话对象再次设置为null。

My initial thought was that this is happening due to the IIS server recycling the objects. 我最初的想法是由于IIS服务器回收对象而发生这种情况。 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 我需要在IIS服务器6.0应用程序池上设置哪些配置参数

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. “无限期保持相同的会话”不是解决此问题的正确方法,也没有摆弄IIS以防止AppDomain卸载。

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: 解决此问题的最简单方法是在应用程序的根目录下使用global.asax文件(如果不存在,Visual Studio可以为您创建一个文件,或者您可以在线找到大量示例来自己创建文件) ,然后覆盖以下方法:

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. C#Web服务的行为类似于C#Asp网站。 IIS unloads it from memory after certain (configurable) time. 特定时间(可配置)后,IIS将其从内存中卸载。 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. 添加pinger,更改IIS时间或将其转换为WCF,以便可以将其托管在自己的WindowsService类实例中。 I prefer the last solution because You get control and it's not a workaround but best practice. 我喜欢最后一种解决方案,因为您可以控制,这不是解决方法,而是最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows服务中托管的C#WCF在5分钟后变为空闲 - c# wcf hosted in windows service goes idle after 5 minutes 闲置30分钟后,使用ADFS身份验证登录的用户将重定向到ADFS服务器 - User logged in using ADFS auth is getting redirected to ADFS server after 30 minutes of idle time C# 字符串变空 - C# string becoming empty WPF C#托盘图标应用程序在30分钟后终止 - wpf c# tray icon app terminates after 30 minutes C#Web服务和静态数据库连接 - C# web service and static DB Connection 如何在不使用c#(asp.net)运行项目的情况下30分钟后自动刷新页面 - how to refresh page after 30 minutes automatically without run the project using c#(asp.net) 对于所有 C# 泛型实例化,静态成员变量是否通用? - Is a static member variable common for all C# generic instantiations? C#幻灯片仅在时间到后显示所有幻灯片/图片框不可见的最后一张图像 - C# Slideshow only displays last image after time is up for all slides/picturebox not becoming visible 从WCF服务派生的进程变得空闲 - Process forked from WCF Service is becoming idle 在Contructor中初始化后,C#静态成员将为null - C# static member gets null after init in Contructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM