2

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>();
        }
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
dreadeddev
  • 268
  • 5
  • 16
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Oct 27 '15 at 17:21
  • I don't understand... The question is about Autofac,therefore surely it should be in the title? otherwise it wouldn't make sense! – dreadeddev Oct 28 '15 at 14:57
  • My comment was regarding the prefixing of title with *MVC6 -*, which I've removed ... –  Oct 28 '15 at 15:04
  • It would be good to post the full exception you're getting. – Travis Illig Oct 28 '15 at 18:32
  • I literally am just gettiing an object reference not set to an instance of the object error on the class that should have been injected – dreadeddev Oct 29 '15 at 16:10
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Oct 30 '15 at 08:06
  • I would try call builder.Populate before you call builder.RegisterModule – Joe Audette Oct 31 '15 at 16:57
  • @AndreasNiedermair No, this is about DI, not a basic .NET null ref, if you read the question :) – dreadeddev Nov 02 '15 at 10:18
  • Thanks @JoeAudette I'll try that today.. – dreadeddev Nov 02 '15 at 10:19
  • Is your repo class public? – Jeff Dunlop Nov 09 '15 at 16:59
  • Yeap! And is referencing the Autofac and Autofac injection nuget packages – dreadeddev Nov 11 '15 at 15:11
  • Did you ever get this figured out? I think it's related to modules, because I recently started using AutofacModules and now property injection isn't working. – Carrie Kendall Apr 02 '18 at 18:17

0 Answers0