参考官方: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.
}
}

net core 3 使用 autofac的更多相关文章

  1. .net core web api + Autofac + EFCore 个人实践

    1.背景 去年时候,写过一篇<Vue2.0 + Element-UI + WebAPI实践:简易个人记账系统>,采用Asp.net Web API + Element-UI.当时主要是为了 ...

  2. NET Core源代码通过Autofac实现依赖注入

    查看.NET Core源代码通过Autofac实现依赖注入到Controller属性   阅读目录 一.前言 二.使用Autofac 三.最后 回到目录 一.前言 在之前的文章[ASP.NET Cor ...

  3. NET Core 3.0 AutoFac替换内置DI的新姿势

    原文:NET Core 3.0 AutoFac替换内置DI的新姿势 .NET Core 3.0 和 以往版本不同,替换AutoFac服务的方式有了一定的变化,在尝试着升级项目的时候出现了一些问题. 原 ...

  4. .net core 依赖注入, autofac 简单使用

    综述 ASP.NET Core  支持依赖注入, 也推荐使用依赖注入. 主要作用是用来降低代码之间的耦合度. 什么是控制反转? 控制反转(Inversion of Control,缩写为IoC),是面 ...

  5. 查看.NET Core源代码通过Autofac实现依赖注入到Controller属性

    一.前言 在之前的文章[ASP.NET Core 整合Autofac和Castle实现自动AOP拦截]中,我们讲过除了ASP.NETCore自带的IOC容器外,如何使用Autofac来接管IServi ...

  6. 在ASP.Net Core下,Autofac实现自动注入

    之前使用以来注入的时候,都是在xml配置对应的接口和实现类,经常会出现忘了写配置,导致注入不生效,会报错,而且项目中使用的是SPA的模式,ajax报错也不容易看出问题,经常会去排查日志找问题. 于是在 ...

  7. .net core中使用autofac进行IOC

    .net Core中自带DI是非常简单轻量化的,但是如果批量注册就得扩展,下面使用反射进行批量注册的 public void AddAssembly(IServiceCollection servic ...

  8. 使用.Net Core Mvc +SqlSugar +Autofac+AutoMapper+....

    开源地址:https://github.com/AjuPrince/Aju.Carefree 目前用到了 SqlSugar.Dapper.Autofac.AutoMapper.Swagger.Redi ...

  9. .net core 2.0 Autofac

    参考自 https://github.com/VictorTzeng/Zxw.Framework.NetCore 安装Autofac,在`project.csproj`加入 <PackageRe ...

  10. net core 2.0 + Autofac的坑

    控制器不能从容器中解析出来; 只是控制器构造函数参数.这意味着控制器生命周期,属性注入和其他事情不由Autofac管理 - 它们由ASP.NET Core管理.可以通过指定AddControllers ...

随机推荐

  1. ValueError: row index was 65536, not allowed by .xls format

    报错:ValueError: row index was 65536, not allowed by .xls format 读取.xls文件正常,在写.xls文件,pd.to_excel()时候会报 ...

  2. Nginx部署前后端分离的单页应用配置

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  3. mysql批量修改数据库表引擎

    数据库表之前的引擎是MyISAM,影响事务操作,要改成Innodb引擎 查询表引擎 SELECT CONCAT(table_name,' ', engine) FROM information_sch ...

  4. 使用Harbor搭建Docker私有仓库

    ip:192.168.0.145 环境设置 防火墙,selinux等,可以使用本章开头的那个shell脚本 其他主机的hosts文件也都添加上 ip hub.aaa.com windows系统的hos ...

  5. Python字符串常用的方法——真心觉得比java,c好用

    # Strings have many methods wo can use rand_string=" life is a beautiful struggle " print( ...

  6. MySQL去除查询结果重复值

    下面先来看看例子: table  id name  1 a  2 b  3 c  4 c  5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多. 比如我想用一条语句查询得到name不重复 ...

  7. Windows 软件使用

    1.CMD 1. 查看端口对应进程 netstat -ano|findstr "443" 2.通过PID 查找对应进程 tasklist|findstr “<PID号> ...

  8. Vue-img-preload

    预加载页面上的图片资源,提高用户体验 效果预览 使用方法 下载vue-img-preload插件 npm install vue-img-preload 配置参数 eachLoaded(functio ...

  9. JavaScript特点有哪些

    JavaScript特点有哪些 JavaScript 文字脚本语言是一种动态的.弱类型的.基于原型的语言,具有内置的支持类型.它的解释器被称为javascript引擎,是浏览器的一部分,广泛用于客户端 ...

  10. Vue.prototype详解

    参考地址:Vue.prototype详解 如果需要设置 全局变量,在main.js中,Vue实例化的代码里添加. 不想污染全局作用域.这种情况下,你可以通过在 原型 上定义它们使其在每个Vue实例中可 ...