I am attempting to use Autofac with a .NET Core console application using the Microsoft.Extensions.DependencyInjection
and Autofac.Extensions.DependencyInjection
packages but the load methods inside my modules never get invoked and leads to Program
being null during resolution. I would have expected them to load either when I called the AddAutofac()
extension method or when the service provider was built.
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
public class Startup
{
private static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddAutofac(builder =>
{
builder.RegisterModule(new MyFirstModule(configuration));
builder.RegisterModule(new MySecondModule(configuration));
});
using (var serviceProvider = services.BuildServiceProvider())
{
var program = serviceProvider.GetService<Program>();
program.Start();
}
}
}