Search code examples
c#asp.netiis-7asp.net-web-api2webapi

WebAPI application_start not executing on IIS server


I am building a new WebAPI service. So far it has one endpoint that calls a method in my Logic project that calls a method in my EF Repository project. It works when I run it in IIS Express in Visual Studio. When I publish it to an IIS site (IIS 7.5), I get a 404 Not Found and it appears as though application_start never runs. I added code to application_start to throw an exception and I never see that exception, just the 404 Not Found. I created the service in Visual Studio 2015. I used the ASP.NET Web Application (.NET Framework) project type. In the dialog that follows that selection, I chose the Empty template and select the checkbox to add folders and core references for Web API. Based on those selections, the project has the following Nuget packages installed: Microsoft.AspNet.WebApi, Microsoft.AspNet.WebApi.Client, Microsoft,AspNet.WebApi.Core, and Microsoft.AspNet.WebApi.WebHost. (It's got a few other packages, but those are all the WebApi packages. My global.asax.cs code looks like this:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        //throw new ApplicationException("This is just a test");
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

And my WebApiConfig.Register method looks like this:

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

But, as I said, I don't believe any of the above code is executing when I deploy the code to IIS. When I un-comment the throw statement in Application_Start, I never see the exception thrown. I've also at times, while debugging this problem, had a Sleep(60000) statement in Application_Start. Run in IIS Express in Visual Studio, I can see that one minute delay. When deployed to IIS it fails instantly with a 404 - no one minute delay.

What would make Application_Start not run?


Solution

  • I found the issue. The new IIS site that was serving up my new service had an app pool that defaulted to .NET Framework v2 instead of v4. When I updated the app pool to v4, the service started working.