Global.asax.cs and Static variable
Asked Answered
H

3

6

In a WCF Service, I need to create a variable which should be accessible anytime from anywhere. 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. However I am not sure to understand what is going to be the scope of that variable. 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. 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...

Halley answered 26/5, 2011 at 16:58 Comment(1)
You do not want a global variable but you want something that is accessible from anybody and everywhere, see the contradiction?Henden
G
5

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.

Guv answered 26/5, 2011 at 17:5 Comment(2)
Personally, I find global untyped Hashtables like Application is worse then a single typed object. Is Application data shared accross app domain, at least?Henden
@codymanix: Yah, this makes the variable universally available across the entire AppDomain. It's not my favorite method of storing this type of information, but for aggregate appdomain specific information it's a good common mechanism to use. It's not the method I use to accomplish this task, but it does work.Guv
H
0

Yes a static variable is valid / visible from all threads / sessions in your application.

AFAIK, static variables are not shared between 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. 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.

Henden answered 26/5, 2011 at 17:5 Comment(0)
S
0

I would use the Singleton pattern to store your "application wide" variable. It is static, will be allocated after the first usage and is available for the lifetime of your application. I also think this is a lot better than using an untyped HashTable like Application. To me Application storage is a relict from ASP and no longer useful in the object oriented world.

Be careful that the static variable is initialized only once, because every web request/service call runs in its own thread.

Like this you could load the data on the first usage and access it from everywhere with 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.

Scherle answered 26/5, 2011 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.