0

I have an ASP.NET MVC 4.5 website, which also exposes a REST API via Web API.

I noticed that 'Application_Start' method called twice - first time when navigating to any URL on website, and second time when calling to any of REST API.

Apparently, both parts are loaded into separate AppDomains. For example, I have Unity dependency injection and I see that two containers are co-exist side by side, each one having a separate set of dependencies.

I just created a clean fresh "Web + Web API" project from VS template, and I see the same behavior - so (apparently) that is not something related to my implementation details.

Such behavior is very unfortunate - because I want to share singleton dependencies between ASP.NET MVC and Web API. Is there any solution to such issue?

Illidan
  • 4,047
  • 3
  • 39
  • 49
  • 1
    I think this is an issue related to IIS. You would still see this issue if you make just an MVC project and leave Web API out. This issue dates all the way back to 2004, maybe even sooner. –  Sep 09 '17 at 05:10
  • It could be your [antivirus](https://stackoverflow.com/questions/29896760/application-start-called-twice-in-iis7-5-hosted-mvc-5-application), [or this](https://stackoverflow.com/questions/3306845/why-would-application-init-fire-twice-when-starting-debugging-in-vs2008-casini). –  Sep 09 '17 at 05:19
  • @Amy: indeed, looks like it somehow related to IIS. I just tried to run my project on IIS Express and seems to works as desired. Very weird... What it could be? – Illidan Sep 09 '17 at 05:37
  • Well, your MVC and Web API are both in the same appdomain. Application_Start is just getting called twice. Use a static singleton for your init and you'll be fine. –  Sep 09 '17 at 05:47

0 Answers0