16

I have face a requirement,

I want client access a data center but without use database , so I want my web app can retain a global or Application session variable, that contains the data, every client can access the same data... I am try to declare in golabl, but seem it only can store String but others ...

how to solve this problem ?

thanks.

3
  • 2
    Are you referring to a session variable that will stay constant through the life of the session or a variable that will be shared between clients (other sessions)? Commented Nov 13, 2010 at 5:28
  • Can you share the example you're trying to work with? Commented Nov 14, 2010 at 1:52
  • See also stackoverflow.com/questions/20347280/… Commented Apr 16, 2016 at 15:41

5 Answers 5

12

To Share the data with all application users, you can use ASP.NET Application object. Given is the sample code to access Application object in ASP.NET:

Hashtable htblGlobalValues = null;

if (Application["GlobalValueKey"] != null)
{
    htblGlobalValues = Application["GlobalValueKey"] as Hashtable;
}
else
{
    htblGlobalValues = new Hashtable();
}

htblGlobalValues.Add("Key1", "Value1");
htblGlobalValues.Add("Key2", "Value2");

this.Application["GlobalValueKey"] = htblGlobalValues;

Application["GlobalValueKey"] can be used anywhere in the whole application by any user. It will be common to all application users.

Sign up to request clarification or add additional context in comments.

3 Comments

Please stop adding signature blocks to your posts. They make it look like you're trying to spam your web site. There's a link to your profile on every post you make, which acts as your signature. You can add anything you like (within reason) to your profile page. That would be the place to post a link to your blog, web site, side projects, business, etc.
You do excess lookup to Application
-1 for no thread-safe locking. See @Pieter's answer for an example of what to add. Whether using Application or a static class, it's still necessary.
12

Another option of defining a global variable is by creating a static class with a static property:

public static class GlobalVariables
{
    public static string MyGlobalVariable { get; set; }
}

You can make this more complex if you are going to use this as a data store, but the same idea goes. Say, you have a dictionary to store your global data, you could do something like this:

public static class GlobalData
{
    private static readonly object _syncRoot = new object();
    private static Dictionary<string, int> _data;

    public static int GetItemsByTag(string tag)
    {
        lock (_syncRoot)
        {
            if (_data == null)
                _data = LoadItemsByTag();

            return _data[tag];
        }
    }

    private static Dictionary<string, int> LoadItemsByTag()
    {
        var result = new Dictionary<string, int>();

        // Load the data from e.g. an XML file into the result object.

        return result;
    }
}

10 Comments

The static data is common only inside the pool, if some have many pools then they are different.
@Aristos - It is static within the AppDomain. Is that the same as within the pool?
@Pieter no the static is the same only inside the pool. If you have 2 pools then you have 2 different static value, and if a user, use both pools that is very possible, then the value is different. Make some tests to see it by your self. (you need iis6+)
@Aristos - That's very interesting. I'm curious then what's the difference between a pool and an AppDomain. Do you know some documentation of that?
@Pieter Look, the pool take care to run one AppDomain for one web site. You can have many web sites on the same pool, and you have many AppDomain for the same web site, each AppDomain in each pool. All that are not connected together.
|
2

You can stuff data into the Application object if you want. It isn't persistent across application instances, but that may sufficient.

(I'm not for a minute going to suggest this is a best practice, but without a clearer picture of the requirements, that's all I can suggest.)

http://msdn.microsoft.com/en-us/library/system.web.ui.page.application.aspx
http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx

Comments

1

If you are using WebApplication or MVC just go to Global.asax (in WebSite project you need to add Global.asax from the add new item menu).
I will explain to deploy two global variables for your web application:
Open the Global.asax file, then define your variable in Application_Start function as following:

void Application_Start(object sender, EventArgs e)
{
   Application.Lock();
   Application["variable1"] = "Some Value for variable1";
   Application["variable2"] = "Some Value for variable2";
   Application.UnLock();
}


If you want to use that those global variables in aspx pages just need to call them like this:

<p>I want to call variable1 <%=Application["variable1"].ToString() %></p>
<p>I want to call variable1 <%=Application["variable2"].ToString() %></p>


But if you want to use that those global variables in server-side call'em like this:

protected void Page_Load(object sender, EventArgs e)
{
   string str1 = Application["variable1"].ToString();
   string str2 = Application["variable2"].ToString();
}

Note: You must be aware that these global variables are public to all users and aren't suitable for authentication jobs.

Comments

0

You can also use Cache, which has advantages like ability to set expire time/date.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.