I have setup an mvc5.2 webapi project and have added all of the dependencies for autofac and owin.
Within my startup class I have the following code which is taken directly from the docs Autofac Owin Doco
The only change I have made is RegisterApiTypes(builder);
public class Startup
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
RegisterApiTypes(builder);
// STANDARD WEB API SETUP:
// Get your HttpConfiguration. In OWIN, you'll create one
// rather than using GlobalConfiguration.
var config = new HttpConfiguration();
// Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// Run other optional steps, like registering filters,
// per-controller-type services, etc., then set the dependency resolver
// to be Autofac.
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// OWIN WEB API SETUP:
// Register the Autofac middleware FIRST, then the Autofac Web API middleware,
// and finally the standard Web API middleware.
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
}
private static void RegisterApiTypes(ContainerBuilder builder)
{
builder.RegisterType<Logger>()
.As<ILogger>()
.InstancePerRequest();
builder.RegisterType<EmailService>()
.As<IEmailService>()
.InstancePerRequest();
}
}
The startup class is called and there isn't any issues however when I try hit a controller I get the
An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.
Should I be setting up the Startup.cs differently?