简体   繁体   中英

ASP.NET Application Object Getters and Setters

I have had a hard time finding a newbie friendly example of accessing the Application Object. I have few things ( arrays ) I would like to store in the Application Object to keep as persistent data for about 2 hours until AppRecycle.

Anyhow, I know how to set an Application Object variable this way:

// One way
String[] users = new String[1000];
Application["users"] = users;

// Another way
Application.Add("users", users);

However I do not know how to access these variables once they are inside the Application Object. There is a Getter method Get . However it requires int index . The other method, Contents , get's everything, not just my array.

Here I try to retrieve my String[] array, but it gives me a error that I am trying to convert Object to String.

String[] usersTable = Application["users"];
// Since this is an object i also tried Application.users but gives error

In Application you only save type "object". You need to cast to the desired type

string[] usersTable = (string[])Application["users"];

You need to cast the value back to String[] :

String[] usersTable = (String[])Application["users"];

See How to: Read Values from Application State

First I suggest for your case to simple use a static variable on the string

static String[] users = new String[1000];

and manipulate that. Is better than Application state. What Microsoft says about :

ASP.NET includes application state primarily for compatibility with classic ASP so that it is easier to migrate existing applications to ASP.NET. It is recommended that you store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary.

Now if you continue to use the Application I strongly suggest to use use Application.Lock(); and Application.UnLock(); when you manipulate the data of the Application and your code will be as:

String[] _users = null;
String[] users
{
    get
    {
        if (_users == null)
        {
            Application.Lock();
            if (!(Application["_users_"] is String[]))
            {
                Application["_users_"] = new String[1000];
            }
            _users = (String [])Application["_users_"];
            Application.UnLock();
        }

        return _users;
    }
}

Why I suggest to simple use a static ?

Because the Application["_users_"] is actually a call to a static Dictionary<k,v> that store among other data, and the data you set as String[]. No actually reason to make that, a static can do the same.

A better solution, IMO, would be use the WebCache (http://msdn.microsoft.com/en-us/library/d59zceh7.aspx) due to the amount of control you can have over expiration and dependencies. This way, if you had the desire to no longer recylce your app-pool, you could still keep the data for a limited, set period of time.

         string[] Cache["users"];

That's all you need to access the cache for your object. Setting it is easy as well. See: http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add(v=vs.100).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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