Solved

Where to store data in ASP.Net

Posted on 2013-11-12
13
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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