Bjarki Heiðar
Bjarki Heiðar

Reputation: 3147

Create a class instance on Application_Start

Is it possible to create a class instance on Application_Start that 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");

My Helper class loads a fairly big XML file to memory and I would like to do that only once.

Upvotes: 3

Views: 1503

Answers (3)

Akram Shahda
Akram Shahda

Reputation: 14771

Use application variables:

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

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

Refer to:

ASP.NET Application State Overview

Upvotes: 4

Eystein Bye
Eystein Bye

Reputation: 5126

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

ManagedWebSessionContext.Bind(HttpContext.Current, globalHelper)

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

What if you put your XML file Application level object in Application_Start event ?

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

Upvotes: 1

Related Questions