I have read the few articles there are on this and now got Autofac working with the Preview version of MVC6 and am injecting a service into my controller! Yaaay!
However I also added a injection in the service (which is a nuget class library) all registered in the StartUp.cs as per recommendation. And it just doesn't work!
It is exactly the same code as injecting the service into the controller, but not working, it injects the service as expected but the very next line which calls the injected repo in the service just gets an object reference error.
"dependencies": {
"Microsoft.CSharp": "4.0.0-beta-23019",
"Autofac": "4.0.0-beta6-110",
"Autofac.Framework.DependencyInjection": "4.0.0-beta6-110",
"Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-beta6",
"SceneStealer.Services.Interfaces": { },
"SceneStealer.Models": { },
"SceneStealer.Data": { },
"SceneStealer.Data.Interfaces": { },
"System.Collections": "4.0.10-beta-23019",
"System.Linq": "4.0.0-beta-23019",
"System.Runtime": "4.0.10-beta-23019",
"System.Threading": "4.0.10-beta-23019"
},
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder
.Register(c => new ConferenceService())
.As<IConferenceService>()
.InstancePerLifetimeScope();
builder.Register(c => new ConferenceRepo())
.As<IConferenceRepo>()
.InstancePerLifetimeScope();
}
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add Application Insights data collection services to the services container.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();#services
//Autofac config
services.AddMvc();
// Create the Autofac container builder.
var builder = new ContainerBuilder();
// Add any Autofac modules or registrations.
builder.RegisterModule(new AutofacModule());
// Populate the services.
builder.Populate(services);
// Build the container.
var container = builder.Build();
// Resolve and return the service provider.
return container.Resolve<IServiceProvider>();
}