描述:
我正在使用 Autofac 创建一个 ASP.NET Core 应用程序,因此我正在关注为它提供的文档:https ://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html ,应该是一个非常简单的路径,但我确实有一个问题,我的 Statup 类继承自 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 类看起来像这样
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 类中注册的任何东西,我设法解决了这个问题,但我想知道发生了什么幕后,
使固定:
我已经删除了 StartupBase 的继承,一切都很好
理论:
我怀疑 .UserStartup 中的 Autofac 以某种方式获取了基类并尝试从中获取方法,但我无法证明这一点,也找不到合适的词来搜索它。有人可以解释一下为什么这里的简单继承是个问题吗?