2

I'm having an issue with a WebAPI project hosted on IIS that serves a REST API. The project uses Autofac to take care of creating the controllers. This works fine and has no issues. However, after a certain amount of time (rather long, hard to time it) when the REST API is called the server returns an error. When doing the same call again everything is back to normal. After research I noticed that Autofac couldn't create instances which results in a 500 error (and thus an error message for the end user).

This is the error message returned from the server (which indicates autofac couldn't create the instances.

An error occurred when trying to create a controller of type 'TranslationController'. Make sure that the controller has a parameterless public constructor.

at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()

Autofac's WebApi extension is used and works (except from the first load).

This is the module that registers the controllers:

public class WebApiIntegrationModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
    }
}

All of the dependencies used by the controllers are in the same assembly, so no other assemblies should be needed to instantiate the controller.

The service actually runs without issues, except that first load. I've been looking around for an answer but I haven't found any. I hope someone here knows where to start looking? This surely has to do something with the initial load of the service in IIS but I cannot see anything wrong. What did I miss?

Steven
  • 166,672
  • 24
  • 332
  • 435
SLX
  • 41
  • 6

1 Answers1

2

I actually fixed this issue by changing the StartMode of the application pool under IIS. It's now set to "AlwaysRunning" so the initial load issue has gone away. I'm not sure if there are better options, but I can surely live with this one (keeping it running isn't an issue for this type of application).

StartMode

Hope this helps someone else!

Update

It seems that the UI setting for this property is only available from IIS 8 and higher, IIS 7.5 (Windows 2008 R2) doesn't show this property in the UI although it's there.

To update it on these version you can use this command (run it in CMD)

%windir%\system32\inetsrv\appcmd set apppool "AppPoolName" -startMode:AlwaysRunning

where AppPoolName is the name of your application pool.

Note that this also works on the newer versions. Creating a bat file of this command should set this property for all versions (that support it).

SLX
  • 41
  • 6