Solved

Where to store data in ASP.Net

Posted on 2013-11-12
13
395 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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