Solved

Where to store data in ASP.Net

Posted on 2013-11-12
13
389 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now