Create an instance of the class on Application_Start

Is it possible to create an instance of a class on Application_Startthat can be used in all controllers?

I would like to be able to create, for example:

var globalHelper = new LoadsStuff();

and then in my action methods:

globalHelper.GetInfoFor("key");

The My Helper class loads a fairly large XML file into memory, and I would like to do this only once.

+3
source share
3 answers

Use application variables:

Application["GlobalHelper"] = new LoadsStuff();

((LoadsStuff) Application["GlobalHelper"]).GetInfoFor("key");

Talk to:

ASP.NET Application Status Overview

+3
source

What if you put your XML file Application level objectin an Application_Startevent?

void Application_Start(object sender, EventArgs e)
{
     Application[""] = //
}
+1
source

Yes, I think you can. I have not tried this, but something like:

ManagedWebSessionContext.Bind(HttpContext.Current, globalHelper)
0
source

All Articles