1
votes

In a simple web api application, I have an OData v3 controller and a Web Api controller both using Autofac. After I got Odata working, my Web Api injection has stopped working with:

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

I've troubleshooted autofac a hundred times and this one has me stumped. I've confirmed that both of the dependencies can be resolved.

Nuget packages installed into Visual Studio 2012:

  • Autofac.WebApi2 3.4.0
  • Autofac 3.5.2
  • Microsoft.Data.Edm 5.6.4
  • Microsoft.AspNet.WebApi 5.2.3
  • Microsoft.Data.OData 5.6.4

Background information: In Visual Studio 2.12 I started with Web Api 2.2, then added Odata v1-3. At this point, both controllers worked. When I installed Autofac, I started getting a message in both controller responses of a missing reference to Microsoft.Odata.Core 6.11. After I installed Microsoft.Odata.Core 6.11 alongside 5.6.4, Odata started working, but Web Api broke.

I have to use Odata v3 to get Excel support. I know Odata v3 is working because Excel is still able to pull data.

ContainerConfig.cs

var builder = new ContainerBuilder();

var config = GlobalConfiguration.Configuration;

builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); 
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
   .AsImplementedInterfaces();

builder.RegisterWebApiFilterProvider(config);

var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Web Api 2.2 controller:

public class UsersController : ApiController
{
    private readonly IUserService _users;

    UsersController(IUserService users)
    {
        _users = users;
    }        
}

Odata controller:

public class InsightsController : ODataController
{
    private readonly IInsightService _insightService;

    public InsightsController(IInsightService insightService)
    {
        _insightService = insightService;
    }        
}
1

1 Answers

0
votes

Doh. It helps to make the constructor public.