简体   繁体   English

为什么静态变量不能在ASP.NET中按预期方式工作?

[英]Why don't static variables work as expected in ASP.NET?

I built an ASP.NET application and a public static variable in it: 我在其中构建了一个ASP.NET应用程序和一个public static变量:

public static WebEndPointFlow[] EndPoints = new WebEndPointFlow[10];

where WebEndPointFlow is a my custom class. 其中WebEndPointFlow是我的自定义类。
The problem is: when I set some element of the array of EndPoints , it only can last for a very short time period. 问题是:当我设置EndPoints数组的某个元素时,它只能持续很短的时间。 After minutes, the element becomes null . 分钟后,该元素变为null
I don't know what happens to my elements in the array. 我不知道数组中的元素会发生什么。 So I create a deathless thread to access these elements regularly, hoping GC won't process them. 因此,我创建了一个永无休止的线程来定期访问这些元素,希望GC不会对其进行处理。 How pathetic, it does not work. 多么可悲,它不起作用。
Any idea on how to remain my original elements always? 关于如何始终保持原始元素的任何想法吗? Very appreciate! 非常感谢!

====================================== =====================================
So the problem does not matter with GC? 那么问题与GC无关吗? I agree. 我同意。
But why my elements become null? 但是为什么我的元素变为空?

=========================== ==========================
One more question: does this array look the same when different users access it? 另一个问题:当不同的用户访问该数组时,它看起来是否相同?
code of Class: 类别代码:

public class WebEndPointFlow
    {
    private static readonly ILog logger = LogManager.GetLogger(typeof(WebEndPointFlow));

    private InstantMessagingFlow flow;

    public InstantMessagingFlow Flow
    {
        get { return flow; }
    }
    private UserEndpointSettings userEndpointSettings;
    private UserEndpoint endPoint;

    public UserEndpoint EndPoint
    {
        get { return endPoint; }
        set { endPoint = value; }
    }

    public StringBuilder Transcript { get; set; }

    private WebSocketServer wsServer;
    private Conversation conversation;

    // position in the array of 'endPoints'
    private int position;
    public int Position
    {
        get { return position; }
        set { position = value; }
    }
    private FailureResponseException e = null;
    private string _TargetLyncUser;
    public string TargetLyncUser
    {
        get { return _TargetLyncUser; }
        set { _TargetLyncUser = value; }
    }
    private bool sendTrans = false;
    public int Duration
    {
        get;
        set;
    }

    public WebEndPointFlow()
    {
    }

Rather than a garbage collector issue ( as comments in your question have pointed out, and you should find Eric Lippert a good source anyway! ), it might be something with Internet Information Services application pool's recycling. 与其说是垃圾收集器问题, 不如说是您的问题中的注释所指出的,无论如何,您都应该找到Eric Lippert! ),这可能与Internet Information Services应用程序池的回收有关。

Default application pool settings will force a recycle after some idle time. 默认的应用程序池设置将在空闲一段时间后强制执行回收。 Also, an application pool can go down if there's a fatal error (like a stack overflow) which can kill the IIS worker process. 另外,如果出现致命错误(例如堆栈溢出),则该应用程序池可能会关闭,该错误会杀死IIS工作进程。 Other situation might be an extreme memory leak. 其他情况可能是严重的内存泄漏。

In summary: 综上所述:

  • Go to your application's application pool settings and try to increase the idle time: ( NOTE: You shouldn't set this setting permantly: I'm suggesting this for debugging purposes in order to let you discover how application pool model works ). 转到应用程序的应用程序池设置,然后尝试增加空闲时间:( 注:您不应永久设置此设置:我建议您将此设置用于调试目的,以便让您发现应用程序池模型的工作原理 )。
  • Add an Application_Error handler in your Global.asax and log exceptions there and see if something is going wrong after some time. 在您的Global.asax添加一个Application_Error处理程序,并在其中记录异常,看一段时间后是否出了问题。

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

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