是静态类实例特有的请求或在ASP.NET服务器? [英] Are static class instances unique to a request or a server in ASP.NET?

查看:90
本文介绍了是静态类实例特有的请求或在ASP.NET服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ASP.NET网站,是唯一的静态类每个Web请求,或者是他们需要的实例,无论何时GCed每当GC决定处置他们?

我想问的原因是因为我在C#编写之前一些静态类和行为是不同的,比我本来期望。我本来期望静态类是唯一的每个请求,但它似乎并不像即是如此。

如果他们不是唯一的每个请求,是有办法让他们呢?

更新:

答案driis给我的是我需要什么。我已经使用一个单独的类,但它使用的是静态实例,因此正在即使用户是不同的,在这种情况下是一件坏事请求之间共享。使用 HttpContext.Current.Items 完美地解决了我的问题。对于任何人谁在后,将来这个问题失蹄,这里是我的实现,简化并缩短使得它很容易理解的方式:

  System.Collections中使用;
使用的System.Web;公共类GloballyAccessibleClass
{
    私人GloballyAccessibleClass(){}    公共静态GloballyAccessibleClass实例
    {
        得到
        {
            IDictionary的项目= HttpContext.Current.Items;
            如果(!items.Contains(TheInstance))
            {
                项目[TheInstance] =新GloballyAccessibleClass();
            }
            退换货品[TheInstance]作为GloballyAccessibleClass;
        }
    }
}


解决方案

您静态类和静态实例字段都要求应用程序之间共享的,并且具有相同的寿命作为应用程序域。因此,你应该使用静态实例时,因为你可能有同步问题之类的要小心。的同时也要记住,静态情况下,不会在应用程序池回收之前由静态实例引用的一切GC'ed,因此,不会GC'ed。这可能会导致内存使用的问题。

如果你需要一个实例的生存期的要求,我会建议使用 HttpContext.Current.Items 集合。这是由设计意味着要存储您需要througout请求的东西的地方。为了更好的设计和可读性,你可以使用Singleton模式,以帮助您管理这些项目。简单地创建在 HttpContext.Current.Items 存储其实例Singleton类。 (在我的ASP.NET共同图书馆,我有这个目的的通用SingletonRequest类)。

On an ASP.NET website, are static classes unique to each web request, or are they instantiated whenever needed and GCed whenever the GC decides to disposed of them?

The reason I ask is because I've written some static classes before in C# and the behavior is different than I would have expected. I would have expected static classes to be unique to each request, but it doesn't seem like that is the case.

If they are not unique to each request, is there a way to allow them to be?

UPDATE:
The answer driis gave me was exactly what I needed. I was already using a singleton class, however it was using a static instance and therefore was being shared between requests even if the users were different which in this case was a bad thing. Using HttpContext.Current.Items solves my problem perfectly. For anyone who stumbles upon this question in the future, here is my implementation, simplified and shortened so that it easy to understand the pattern:

using System.Collections;
using System.Web;

public class GloballyAccessibleClass
{
    private GloballyAccessibleClass() { }

    public static GloballyAccessibleClass Instance
    {
        get
        {
            IDictionary items = HttpContext.Current.Items;
            if(!items.Contains("TheInstance"))
            {
                items["TheInstance"] = new GloballyAccessibleClass();
            }
            return items["TheInstance"] as GloballyAccessibleClass;
        }
    }
}

解决方案

Your static classes and static instance fields are shared between all requests to the application, and has the same lifetime as the application domain. Therefore, you should be careful when using static instances, since you might have synchronization issues and the like. Also bear in mind, that static instances will not be GC'ed before the application pool is recycled, and therefore everything that is referenced by the static instance, will not be GC'ed. This can lead to memory usage problems.

If you need an instance with the same lifetime as a request, I would suggest to use the HttpContext.Current.Items collection. This is by design meant to be a place to store stuff that you need througout the request. For nicer design and readability, you can use the Singleton pattern to help you manage these items. Simply create a Singleton class that stores its instance in HttpContext.Current.Items. (In my common library for ASP.NET, I have a generic SingletonRequest class for this purpose).

这篇关于是静态类实例特有的请求或在ASP.NET服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆