Vishal Seth
Vishal Seth

Reputation: 5048

ASP.NET Application variable vs reading from ConfigurationSettings.AppSettings

There is a similar question posted at Application Variable Vs Web.Config Variable, but mine is slightly different and I'm not convinced with the answer there.

So, if I define an application variable in my global.asax like this:

public class Global : System.Web.HttpApplication
{    
   public static readonly string X =  ConfigurationSettings.AppSettings["XsltExternal"];
   // rest of the code here
}

Shouldn't this read operation

string Y = Global.X;

be faster than

string Y = ConfigurationSettings.AppSettings["XsltExternal"];

because of avoiding hashtable lookup (presuming that's how ASP.Net stores the web.config settings)? My application uses tons of config settings and checks for it throughout page cycle so I'm hoping I to take advantage of every single ms that I can save.

Thoughts anybody?

PS: My initial simple-test-page ANTS profiler test shows read time dropping from 0.017 ms to .002 ms.

Upvotes: 2

Views: 1872

Answers (1)

covo
covo

Reputation: 540

I would say yes, it is faster, but I would try to keep the Global class as clean as possible. In my implementations I usually put all my configuration items on a separate class with a static constructor, for example:

public class Constants
{
    public static string PayPalSeller;
    public static string PayPalUrl;
    public static string PayPalPDTKey;
    public static string RpxTokenUrl;
    public static string VirtualAppFolder;
    static Constants()
    { 
        PayPalSeller = ConfigurationManager.AppSettings["PayPalSeller"];
        PayPalUrl = ConfigurationManager.AppSettings["PayPalUrl"];
        PayPalPDTKey = ConfigurationManager.AppSettings["PayPalPDTKey"];
        RpxTokenUrl = ConfigurationManager.AppSettings["RpxTokenUrl"];
    }
 }

To use it, of course you would go:

Constants.PayPalSeller

hope this helps, -covo

Upvotes: 3

Related Questions