Developer forum

Forum » Development » Application wide variables

Application wide variables

Dmitrij Jazel
Reply

 Edit: Wops, wrong category... could someone please move this post to Developmet section?

 

Hej DW guys,

 

I have a question here,

 

Let's say I have some data, that must be available withing application. For example Application Variables could store some of it.

For example store a set that must be accessed really quick, and to take some load of the database I was thinking to put this Dictionary

 

I was trying to use something like this in DW:

Application["CachedValues"] = cachedList;
Dictionary<string, double> cachedList = (Dictionary<string, double>)Application["CachedValues"];

But it does not seem to work.

Dynamicweb.Cache Cache = new Dynamicweb.Cache();

This one either :-/

 

Any reason why? or maybe there are more suggestions how to achieve something like this?

P.S. I am full aware about application variables side-effect - that they can drain the memory on the server, so must be treated with caution.

But this variable I am working with, may not change for 24-48 hours (it must be constantly accessible globally), and if it does, I will change it directly, so it must be alive as long as application is alive. Just 1. It would help performance alot, as I could save additional connection to database and list through 1000 values.

 

Many thanks in advance! :-)

 

Regards,

Dmitrij

 

 


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Can you define "does not seem to work"? Your current code wouldn't compile because the order is incorrect, but assuming you have this in the right order in your code, I see no reason why this wouldn't work. This is standard ASP.NET and not really related to Dynamicweb.

Can you post your full code and a better problem description?

Cheers,

Imar

 
Dmitrij Jazel
Reply

Hej Imar,

 

Sure thing, this is how my CodeBehind file contains, when I create small webform application on my local machine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cacheLocalTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["CachedValues"] == null)
            {
                Button1.Text = "!empty!";
            }
            else
            {
                Dictionary<string, double> prices = (Dictionary<string, double>)Application["CachedValues"];
                Dictionary<string, double> prices2 = (Dictionary<string, double>)Application["CachedValues"];
                Button1.Text = "!FULL!";

                foreach (KeyValuePair<string, double> pair in prices)
                {
                    Button1.Text = pair.Key + pair.Value.ToString();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, double> prices = new Dictionary<string, double>();
            prices.Add("First", 1.1);
            prices.Add("Second", 2.2);
            prices.Add("Third", 3.3);
            Application["CachedValues"] = prices;
        }
    }
}

And this one works fine. But when I am trying to use it in the example below - than it just gives me a red underline, compiler should suggest some import or something like that, or a reason why I can't use it. But I can't seem to find it.

Here is the code where I need to use it... here I am getting the error... Again - the error is just red underline with suggestion to use System.Net.Mime.MediaTypeNames.Application 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Caching;

namespace IMPORT_SERVICE
{
    public class DBService
    {
		// other database service methods.....

        internal void reCache()
		{
            //1) load user names
            List<UserInfoHolder> userInfo = getAllUserInformation();
            List<string> allProductsIDs = getAllProductsIDs();

            foreach (UserInfoHolder u in userInfo)
            {
                Dynamicweb.Cache cacheTest = new Dynamicweb.Cache();
                string appMsg;
                appMsg = (string)(Application["Message"]);
				
                //Cache["Key1"] = ""; 
				/*Application["MyTest"] = "";*/
            }
			Dictionary<string,float> priceValues = new Dictionary<string,float>();	
				// Populate priceValues
            Application["prices"] = priceValues;

            //2) foreach username
				//2.1 filter discount table - call DB and compare prices on the fly.
		}
	}
}

 

I feel that I am doing something wrong here, but it seams so easy to actually use Application variables, just like Session state, but for some reason - just getting compiler error with suggestion to use System.Net.Mime.MediaTypeNames.Application - that is not something I need to use... I am using System.web reference, so it should be fine :-/

 

Regards,

Dmitrij

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Application is made available as a property on ASPX pages, User Controls and Master Pages so you can use it directly. To use it in other classes such as class files inside a separae class library, you need to use HttpContext.Current.Application instead. Obviously, you can only use it when there is an actual HttpContext available.

 

Hope this helps,

 

Imar

Votes for this answer: 1
 
Dmitrij Jazel
Reply

Hej Imar,

 

That looks like it is working so far! :-)

I will let you know here if there are any other issues but I don't think that there will be any regarding this topic.

I must thank you again for lightning fast help! :-)))

 

Regards,

Dmitrij

 

You must be logged in to post in the forum