Search code examples
c#asp.net-coredependency-injectionautofacautofac-configuration

Autofac & ASP.NET Core : why ASP.NET Core cannot reach ConfigureContainer method at Startup inheriting from StartupBase?


Description:

I am creating a ASP.NET Core application using Autofac, so I am following the documentation given for it: https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html, should a be pretty straightforward path to follow, but I do have a problem, my Statup class inherits from Microsoft.AspNetCore.Hosting.StartupBase which makes me implement the next :

public abstract class StartupBase : IStartup
{
      protected StartupBase();

      public abstract void Configure(IApplicationBuilder app);
      public virtual void ConfigureServices(IServiceCollection services);
      public virtual IServiceProvider CreateServiceProvider(IServiceCollection services);
}

so far everything fine, my Statup class looks like this

public class Startup : StartupBase
{
  readonly IHostingEnvironment hostingEnvironment;

  public Startup(IHostingEnvironment hostingEnvironment)
  {
     //some stuff here
  }

  public override void ConfigureServices(IServiceCollection services)
  {
     //some other stuff here
  }

  // https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html
  public void ConfigureContainer(ContainerBuilder builder)
  {
     builder.RegisterModule(new AutofacModule());        
  }

  public override void Configure(IApplicationBuilder app)
  {
     //more stuff here
  }     
}    

and the Program.cs, just for example purpose this is some example

public class Program
{
  public static void Main(string[] args)
  {
    // The ConfigureServices call here allows for
    // ConfigureContainer to be supported in Startup with
    // a strongly-typed ContainerBuilder.
    var host = new WebHostBuilder()
        .UseKestrel()
        .ConfigureServices(services => services.AddAutofac())
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
  }
}

Problem:

Once the application starts running it should hit the ConfigureContainer method, but it doesn´t and I do not know why, which means that I cannot inject anything registered in the AutofactModule class, I manage to solve this but I want to know what is happening behind scenes,

Fix:

I have deleted the inheritance from StartupBase and everything went fine

Theory:

I suspect that somehow Autofac in the .UserStartup gets the base class and tries to get the methods from it, but I cannot prove it and could not find the right words to search for it. Can somebody explain me why the simple inheritance is a problem here?.


Solution

  • So, this is actually more a microsoft thing than an Autofac thing - within the Microsoft.AspNetCore.Hosting namespace is a StartupLoader Class Github Here. This is what actually chooses which methods are called. It has an interesting quirk (design choice) of not calling the Autofac methods - there have been a couple of related issues raised on the github though have generally been closed as it seems to be a design choice that they dont intend to change