简体   繁体   English

Global.asax.cs 和 Static 变量

[英]Global.asax.cs and Static variable

In a WCF Service, I need to create a variable which should be accessible anytime from anywhere.在 WCF 服务中,我需要创建一个可以随时随地访问的变量。 All methods of my service need to have access to that data and I can load it only once.我服务的所有方法都需要访问该数据,并且我只能加载一次。 So I though about using a static variable in the Global.asax.cs.所以我想在 Global.asax.cs 中使用 static 变量。 However I am not sure to understand what is going to be the scope of that variable.但是我不确定该变量的 scope 将是什么。 Is that data is going to be used for all the requests?该数据是否将用于所有请求? My understanding is that it should because the same static variable should be used across the App Domain.我的理解是它应该是因为应该在整个应用程序域中使用相同的 static 变量。 Is that correct?那是对的吗?

public static IList<MyData> Data { get; set; } 
private static IList<MyData> Load() 
{
    return Big data struct from DB.
}

protected void Application_Start(object sender, EventArgs e)
{
    Data = Load();
}

Finally, is there a better way to achieve that?最后,有没有更好的方法来实现这一目标? I am not a big fan of static variable...我不是 static 变量的忠实粉丝......

You could use an application variable:您可以使用应用程序变量:

public static IList<MyData> Data {
    get
    {
        if (Application["MyIListData"] != null)
            return (IList<MyData>)Application["MyIListData"];
        else
            return new IList<MyData>();
    }
    set
    {
        Application["MyIListData"] = value;    
    }
} 

protected void Application_Start(object sender, EventArgs e)
{
    Data = Load();
}

Not really much different in actual implementation except that now it's available globally through that variable name as an application variable.在实际实现中并没有太大的不同,只是现在它可以通过该变量名作为应用程序变量在全局范围内使用。

Yes a static variable is valid / visible from all threads / sessions in your application .是的,static 变量在应用程序中的所有线程/会话中有效/可见。

AFAIK, static variables are not shared between AppDomains . AFAIK,static 变量不在AppDomains之间共享。 For accomplishing this task you may have a look at this example .为了完成这个任务,你可以看看这个例子

You do not want a global variable but you want something that is accessible from anybody and everywhere, you see the contradiction?你不想要一个全局变量,但你想要任何人随处都可以访问的东西,你看到矛盾了吗? Any kind of singleton is just a global variable.任何类型的singleton都只是一个全局变量。 But in your case it seem to be the best solution.但在你的情况下,它似乎是最好的解决方案。 You just should make sure that your global object is immutable and thread safe .你只需要确保你的全局 object 是不可变的和线程安全的。

I would use the Singleton pattern to store your "application wide" variable.我会使用 Singleton 模式来存储您的“应用程序范围”变量。 It is static, will be allocated after the first usage and is available for the lifetime of your application.它是 static,将在第一次使用后分配,并在您的应用程序的生命周期内可用。 I also think this is a lot better than using an untyped HashTable like Application.我也认为这比使用无类型的 HashTable(如 Application)要好得多。 To me Application storage is a relict from ASP and no longer useful in the object oriented world.对我来说,应用程序存储是 ASP 的遗留物,在面向 object 的世界中不再有用。

Be careful that the static variable is initialized only once, because every web request/service call runs in its own thread.注意 static 变量只初始化一次,因为每个 web 请求/服务调用都在其自己的线程中运行。

Like this you could load the data on the first usage and access it from everywhere with MyData.Data:像这样,您可以在第一次使用时加载数据并使用 MyData.Data 从任何地方访问它:

public class MyData
{
    private static IList<MyData> _data { get; set; } 

    public static IList<MyData> Data 
    {    
        get
        {
            if (_data == null)
               _data = load Big data struct from DB.
            return _data;
        }
    }
}

Event better would be an initialisation in the static constructor because then the call would be thread-safe.更好的事件是在 static 构造函数中进行初始化,因为这样调用将是线程安全的。

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

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