Is MvcApplication singleton in a MVC 3 Web Application? Why did I find 3 instances?
My steps: (VS2010 SP1 + ASP.NET MVC 3 Tools Update)
- create a default Internet MVC 3 Appliction (with the account sample)
- Add a MvcApplication() constructor in global.asax, and set a breakpoint in it
- Press F5 to debug
I found the constructor was called 3 times!
Best regards,
Zach@Shine
===Edit===
Correction:
2 instances, not 3 were found for 1 web app.
After a careful check, I found my solution contains 2 web apps, after I removed one of them, I can find 2 instances for the left app.
Here is the log (the numbers are the MvcApplication instances’ hashcode)
MvcApplication(): 41516761
Application_Start(): 41516761
MvcApplication(): 56552081
Application_AuthenticateRequest(): 56552081
Application_AuthenticateRequest(): 56552081
Application_AuthenticateRequest(): 56552081
MvcApplication_Disposed(): 56552081
Application_End(): 41516761
As shown, one instance is used to handle Start, End events, and the other one is used to handle requests.
MvcApplication inherits from HttpApplication from the normal asp.net below is a great link to understanding the asp.net pipeline. I have copied the relevant section from the article.
In short – no MvcApplication is not singleton but there is only ever one present with each request.
http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp