参考官方:https://docs.autofac.org/en/latest/integration/aspnetcore.html#startup-class
有一些变动,现在暂时还没用net core3 做项目
public class Program { public static void Main(string[] args) { // ASP.NET Core 3.0+: // The UseServiceProviderFactory call attaches the // Autofac provider to the generic hosting mechanism. var host = Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>(); }) .Build(); host.Run(); } }
在Startup类中(在所有版本的ASP.NET Core中基本相同),然后使用ConfigureContainer访问Autofac容器生成器并直接向Autofac注册。
public class Startup { public Startup(IHostingEnvironment env) { // In ASP.NET Core 3.0 `env` will be an IWebHostingEnvironment, not IHostingEnvironment. var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); this.Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; private set; } public ILifetimeScope AutofacContainer { get; private set; } // ConfigureServices is where you register dependencies. This gets // called by the runtime before the ConfigureContainer method, below. public void ConfigureServices(IServiceCollection services) { // Add services to the collection. Don't build or return // any IServiceProvider or the ConfigureContainer method // won't get called. services.AddOptions(); } // ConfigureContainer is where you can register things directly // with Autofac. This runs after ConfigureServices so the things // here will override registrations made in ConfigureServices. // Don't build the container; that gets done for you by the factory. public void ConfigureContainer(ContainerBuilder builder) { // Register your own things directly with Autofac, like: builder.RegisterModule(new MyApplicationModule()); } // Configure is where you add middleware. This is called after // ConfigureContainer. You can use IApplicationBuilder.ApplicationServices // here if you need to resolve things from the container. public void Configure( IApplicationBuilder app, ILoggerFactory loggerFactory) { // If, for some reason, you need a reference to the built container, you // can use the convenience extension method GetAutofacRoot. this.AutofacContainer = app.ApplicationServices.GetAutofacRoot(); loggerFactory.AddConsole(this.Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseMvc(); } }
配置方法命名约定
Configure、ConfigureServices和ConfigureContainer方法都支持基于应用程序中IHostingEnvironment.EnvironmentName的特定于环境的命名约定。默认情况下,名称为Configure、ConfigureServices和ConfigureContainer。如果需要特定于环境的设置,可以将环境名称放在配置部分之后,如ConfigureDevelopment、ConfigureDevelopmentServices和ConfigureDevelopmentContainer。如果一个方法没有与环境匹配的名称,那么它将回到默认值。
这意味着您不必使用Autofac配置在开发环境和生产环境之间切换配置;您可以在启动时以编程方式设置它。
public class Startup { public Startup(IHostingEnvironment env) { // Do Startup-ish things like read configuration. } // This is the default if you don't have an environment specific method. public void ConfigureServices(IServiceCollection services) { // Add things to the service collection. } // This only gets called if your environment is Development. The // default ConfigureServices won't be automatically called if this // one is called. public void ConfigureDevelopmentServices(IServiceCollection services) { // Add things to the service collection that are only for the // development environment. } // This is the default if you don't have an environment specific method. public void ConfigureContainer(ContainerBuilder builder) { // Add things to the Autofac ContainerBuilder. } // This only gets called if your environment is Production. The // default ConfigureContainer won't be automatically called if this // one is called. public void ConfigureProductionContainer(ContainerBuilder builder) { // Add things to the ContainerBuilder that are only for the // production environment. } // This is the default if you don't have an environment specific method. public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { // Set up the application. } // This only gets called if your environment is Staging. The // default Configure won't be automatically called if this one is called. public void ConfigureStaging(IApplicationBuilder app, ILoggerFactory loggerFactory) { // Set up the application for staging. } }