Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an 'MvcApplication' instance in ASP.NET controller?

Tags:

I think MvcApplication is a global singleton. I want to get the instance of MvcApplication in the controller. Then I put the following code in controller:

MvcApplication app = HttpContext.Current.Application as MvcApplication; 

It gives me an error:

Error 2 'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found (are you missing a using directive or an assembly reference?)

Why? How do I access MvcApplication in the controller?

like image 416
KentZhou Avatar asked Jun 30 '09 22:06

KentZhou


People also ask

What is ActionResult () in MVC?

An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result.


1 Answers

MvcApplication != singleton

which means that all answers above miss the point and will get you into serious trouble if you wrongly believe to access the same instance while in fact there will be several.

Your very first assumption is not valid: Contrary to (very - just see the other answers here as a proof) widespread belief, MvcApplication is NOT a global singleton. The class is instantiated several times, one instance per "pipeline", so the performance counter "pipeline instance count" tells you how many instances of MvcApplication are currently consdidered alive. Add a default ctor and prove this yourself:

public MvcApplication() {     Trace.WriteLine(this.GetHashCode()); } 

Debug break the line or watch the various hash codes in DebugViewer. To force pipeline instance count going up create a method with Thread.Sleep(5000), Asp.Net will then fire up a new instance once you make another http request in parallel.

Solution - How to instantiate singletons in Asp.Net applications (MVC or WebForms)

If your MvcApplication class however has an Application_Start() method then this method is called in fact only once, process wide. You can set static fields there. Simply put them to any class, typically MvcApplication is a good conventional choice, and access them. Like

MvcApplication.MySingleValue = 72; MvcApplication.ActivePlayersCount = 3400; var n = MvcApplication.ActivePlayersCount; ... 

HttpApplication weirdness

The design of the HttpApplication class and its events is quite strange, which presumably has its reason in some loose sort of backwards design compatibility to very old COM based ASP pages. There the application object was in fact created only once, which is surely the origin of the wrong belief related to Asp.Net. An example of the HttpApplication strangeness:

protected void Application_Start() { } 

Note that there is no override involved!

In summary, the application instances might be of minor interest most of the time, I can see no scenario were it could become relevant to hold state, as its state would be shared by an arbitrary subset of requests handled. So accessing it in the completely fine way as mentioned by Matt might not be required too often.

like image 111
citykid Avatar answered Oct 09 '22 18:10

citykid