net core 3 使用 autofac
参考官方: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的更多相关文章
- .net core web api + Autofac + EFCore 个人实践
1.背景 去年时候,写过一篇<Vue2.0 + Element-UI + WebAPI实践:简易个人记账系统>,采用Asp.net Web API + Element-UI.当时主要是为了 ...
- NET Core源代码通过Autofac实现依赖注入
查看.NET Core源代码通过Autofac实现依赖注入到Controller属性 阅读目录 一.前言 二.使用Autofac 三.最后 回到目录 一.前言 在之前的文章[ASP.NET Cor ...
- NET Core 3.0 AutoFac替换内置DI的新姿势
原文:NET Core 3.0 AutoFac替换内置DI的新姿势 .NET Core 3.0 和 以往版本不同,替换AutoFac服务的方式有了一定的变化,在尝试着升级项目的时候出现了一些问题. 原 ...
- .net core 依赖注入, autofac 简单使用
综述 ASP.NET Core 支持依赖注入, 也推荐使用依赖注入. 主要作用是用来降低代码之间的耦合度. 什么是控制反转? 控制反转(Inversion of Control,缩写为IoC),是面 ...
- 查看.NET Core源代码通过Autofac实现依赖注入到Controller属性
一.前言 在之前的文章[ASP.NET Core 整合Autofac和Castle实现自动AOP拦截]中,我们讲过除了ASP.NETCore自带的IOC容器外,如何使用Autofac来接管IServi ...
- 在ASP.Net Core下,Autofac实现自动注入
之前使用以来注入的时候,都是在xml配置对应的接口和实现类,经常会出现忘了写配置,导致注入不生效,会报错,而且项目中使用的是SPA的模式,ajax报错也不容易看出问题,经常会去排查日志找问题. 于是在 ...
- .net core中使用autofac进行IOC
.net Core中自带DI是非常简单轻量化的,但是如果批量注册就得扩展,下面使用反射进行批量注册的 public void AddAssembly(IServiceCollection servic ...
- 使用.Net Core Mvc +SqlSugar +Autofac+AutoMapper+....
开源地址:https://github.com/AjuPrince/Aju.Carefree 目前用到了 SqlSugar.Dapper.Autofac.AutoMapper.Swagger.Redi ...
- .net core 2.0 Autofac
参考自 https://github.com/VictorTzeng/Zxw.Framework.NetCore 安装Autofac,在`project.csproj`加入 <PackageRe ...
- net core 2.0 + Autofac的坑
控制器不能从容器中解析出来; 只是控制器构造函数参数.这意味着控制器生命周期,属性注入和其他事情不由Autofac管理 - 它们由ASP.NET Core管理.可以通过指定AddControllers ...
随机推荐
- SpreadJS:一款高度类似Excel的开发工具,功能涵盖Excel的 95% 以上
Excel 作为一款深受用户喜爱的电子表格工具,借助其直观的界面.出色的计算性能.数据分析和图表,已经成为数据统计领域不可或缺的软件之一. 基于Excel对数据处理与分析的卓越表现,把Excel的功能 ...
- System x 服务器制作ServerGuide U盘安装Windows Server 2008 操作系统 --不格式化盘
1.全格式化 用ServerGuide10.5 刻录成U盘 下载附件中的Rufus 3.6工具,并制作引导U盘 以管理员权限打开Rufus 3.6, 选择镜像文件 2.不格式化,仅安装C盘下载老毛桃U ...
- 什么是阿里云SCDN
简介 SCDN(Secure Content Delivery Network),即拥有安全防护能力的CDN服务,提供稳定加速的同时,智能预判攻击行为,通过智能的调度系统将DDoS攻击请求切换至高防I ...
- AtCoder M-SOLUTIONS 2019 Task E. Product of Arithmetic Progression
problem link Official editorial: code: int main() { #if defined LOCAL && !defined DUIPAI ifs ...
- 洛谷 P4198 楼房重建 线段树维护单调栈
P4198 楼房重建 题目链接 https://www.luogu.org/problemnew/show/P4198 题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上 ...
- DP_Milking Time
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...
- 网络流Dinic--模板
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>//sprintf islower isupp ...
- MySQL 聚合函数(一)聚合(组合)函数概述
MySQL版本:5.7+ 本节介绍对值的集合进行操作的组合(聚合)函数.翻译自:Aggregate (GROUP BY) Function Descriptions 一.MySQL 5.7中的聚合函数 ...
- LeetCode:197.上升的温度
题目链接:https://leetcode-cn.com/problems/rising-temperature/ 题目 给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日 ...
- c#调用存储过程实现批量增加和修改数据
1 例如当我在编辑表格数据,一次编辑了多行需要保存到数据库时,就需要判断数据库中是否已经存在,存在则修改,不存在则新增一条或多条数据,即所谓批量增加或者跟新数据. 首先需要构建数据包,把要添加或者跟新 ...