Autofac & ASP.NET Core : 为什么 ASP.NET Core 无法在从 StartupBase 继承的 Startup 中访问 ConfigureContainer 方法?

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

描述:

我正在使用 Autofac 创建一个 ASP.NET 核心应用程序,因此我遵循为其提供的文档:https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html,应该是一条非常简单的路径,但我确实遇到了问题,我的 Statup class 继承自 Microsoft.AspNetCore.Hosting.StartupBase 这让我实现了下一个:

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);
}

到目前为止一切正常,我的 Statup class 看起来像这样

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
  }     
}    

和 Program.cs,仅供参考,这是一些示例

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();
  }
}

问题:

应用程序启动后 运行 它应该会调用 ConfigureContainer 方法,但它不会,我也不知道为什么,这意味着我无法注入在 AutofactModule class 中注册的任何内容,我设法解决了这个问题,但我想知道幕后发生了什么,

修复:

我已经删除了 StartupBase 的继承,一切正常

理论:

我怀疑 .UserStartup 中的 Autofac 以某种方式获取基 class 并试图从中获取方法,但我无法证明这一点,也找不到合适的词来搜索它。 有人能解释一下为什么简单继承在这里是个问题吗?

所以,这实际上更像是微软的东西而不是 Autofac 的东西 - 在 Microsoft.AspNetCore.Hosting 命名空间中是 StartupLoader Class Github Here。这实际上是选择调用哪些方法。它有一个有趣的怪癖(设计选择)不调用 Autofac 方法 - github 上提出了几个相关问题,但通常已关闭,因为它似乎是他们不打算的设计选择变化