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

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

问题描述

在 ASP.NET 网站上,静态类对于每个 Web 请求是唯一的,还是在需要时实例化并在 GC 决定处理它们时进行 GC?

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?

我问的原因是因为我之前用 C# 编写了一些静态类,并且行为与我预期的不同.我原以为每个请求的静态类都是唯一的,但事实并非如此.

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?

更新:
德里斯给我的答案正是我所需要的.我已经在使用单例类,但是它使用的是静态实例,因此即使用户不同,它也会在请求之间共享,这在这种情况下是一件坏事.使用 HttpContext.Current.Items 完美地解决了我的问题.对于将来偶然发现这个问题的任何人,这里是我的实现,简化和缩短以便于理解模式:

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;
        }
    }
}

推荐答案

您的静态类和静态实例字段在对应用程序的所有请求之间共享,并且与应用程序域具有相同的生命周期.因此,在使用静态实例时应该小心,因为您可能会遇到同步问题等.还要记住,在应用程序池回收之前,静态实例不会被 GC 处理,因此静态实例引用的所有内容都不会被 GC 处理.这可能会导致内存使用问题.

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.

如果您需要一个与请求具有相同生命周期的实例,我建议使用 HttpContext.Current.Items 集合.这是设计上用来存储您在整个请求中需要的东西的地方.为了更好的设计和可读性,您可以使用单例模式来帮助您管理这些项目.只需创建一个 Singleton 类,将其实例存储在 HttpContext.Current.Items 中.(在我的 ASP.NET 公共库中,我有一个通用的 SingletonRequest 类用于此目的).

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天全站免登陆