Solved

Where to store data in ASP.Net

Posted on 2013-11-12
13
390 Views
Last Modified: 2013-11-12
I need to store a value in ASP.net web application that is assessable "out of band".  The application has processes that run on the timer event, and during these times, there may not be a Request context available.  Is there somewhere I can store a variable since the App or Session cache is not available to me?
0
Comment
Question by:No1Coder
13 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 39641632
You can store data in a database of some sort.  If that's too extravagant, perhaps just saving the info in a text file will do.

As for the client side, you can usually set a cookie.
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 39641646
You can use Static class's static property to store them and access at later point of time.


using System;
using System.Data;
using System.Linq;
using System.Web;

public static class MyClass
{
    static string _importantData;

    public static string ImportantData
    {
	get
	{
	    return _importantData;
	}
	set
	{
	    _importantData = value;
	}
    }
}


//Set the value MyClass.ImportantData = "my data"; and get it later point of time.

Open in new window




If the data are more you could probably choose the option to store data in database such as MSSQL or Access. It depends on your requirement.

If you prefer for file storage I suggest to store and retreive as XML file instead of Flat file.
0
 

Author Comment

by:No1Coder
ID: 39641683
The static approach is very interesting.  Where is this data stored?  Is it visible everywhere?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39641743
If you use a static it will be stored in the App Domain (i.e. the ASP.Net Worker process). If the process gets recycled you will lose the value.

So whether you take that approach or not will depend on how long you need the value to persist.
0
 

Author Comment

by:No1Coder
ID: 39641775
Ok.  What is the visibility?  Is it all processes?  App Pool related?

I need it to be application wide (all sessions running this app), but not all applications running on this pool.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39641850
Static is scoped to the application, same as if you set an Application variable (which personally I would do instead of using a static as it is clearer what the intention was). So, no, it won't be shared by other applications in the same Pool.
0
 

Author Comment

by:No1Coder
ID: 39641896
How would I access the Application object from a static method that is called out of bounds?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39641939
What do you mean by "out of bounds"? If you mean from a third-part process then you can't. You would need to persist the value to disk, or a database, or somewhere to access it.
0
 

Author Comment

by:No1Coder
ID: 39641951
As I explained in my original question, a process running off of the timer event.  No request context available.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39642018
If that timer is running within the context of your web app then you should still be able to get to the context using something like:
string someValue HttpContext.Current.Application["YourVariable"];

Open in new window

That would require you to store your value in an Application variable though, rather than using a static.
0
 

Author Comment

by:No1Coder
ID: 39642256
What I discovered is that when running off the timer, there is no HttpContext available (it is null).  Otherwise, I would use the Application cache.

So, if this is not available, will the static variable method work?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39642373
It should do. I used this test code:
    public static class Members
    {
        public static string Value;
    }

    public class Global : System.Web.HttpApplication
    {
        System.Threading.Timer timer = null;

        protected void Application_Start(object sender, EventArgs e)
        {
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(Timer_Callback), null, 60000, 60000);
        }

        protected void Timer_Callback(object state)
        {
            string value = Members.Value;
        }
    }

Open in new window

And set the variable from a page, and I could retrieve it ok when the timer fired.
0
 

Author Comment

by:No1Coder
ID: 39642801
Okay.  I implemented this.  Thanks a lot for your assistance.  This is much easier than the alternative.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 25
Gridview selected row 9 47
ASP.net Show "please wait " message as page loads 2 42
Set value of dynamically created checkboxlist 2 34
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question