DotNETCore 学习笔记 依赖注入和多环境
2024-08-29 17:06:04
Dependency Injection
------------------------------------------------------------------------
ASP.NET services can be configured with the following lifetimes: Transient
Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services. Scoped
Scoped lifetime services are created once per request. Singleton
Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance. If your application requires singleton behavior, allowing the services container to manage the service’s lifetime is recommended instead of implementing the singleton design pattern and managing your object’s lifetime in the class yourself. **********************************************************************************
"dependencies" : {
"Autofac": "4.0.0",
"Autofac.Extensions.DependencyInjection": "4.0.0"
}, public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// add other framework services // Add Autofac
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterModule<DefaultModule>();
containerBuilder.Populate(services);
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
} public class DefaultModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CharacterRepository>().As<ICharacterRepository>();
}
} ********************************************************************************** Working with Multiple Environments ASPNETCORE_ENVIRONMENT:Development, Staging, and Production env.IsEnvironment("environmentname")
or
env.EnvironmentName == "Development" Startup{EnvironmentName} (for example StartupDevelopment) Configure{EnvironmentName}() ConfigureDevelopment() Configure{EnvironmentName}Services(). ConfigureDevelopmentServices()
最新文章
- Linux虚拟机中配置JDK环境变量
- GitHub 上 57 款最流行的开源深度学习项目
- [Bug]枚举数组,并找到某些元素删除
- putty自动登录
- WebActivatorEx 注入时的使用
- 操作系统:cpu调度 6-25
- H264转成RGB24格式-2016.01.21
- 01-01-01【Nhibernate (版本3.3.1.4000) 出入江湖】配置文件
- 2014-9-17二班----7 web project
- nginx配置:location配置方法及实例详解
- timestamp ---自动更新修改时间 与 记录首次插入时间
- win7系统下连接使用mac 蓝牙键盘(Apple Wireless Keyborad)
- window wamp下xhprof的安装使用,Graphviz配置
- Generator函数语法解析
- 针对Chrome谷歌等浏览器不再支持showModalDialog的解决方案
- iOS中 支付宝钱包详解/第三方支付 韩俊强的博客
- 如何彻底禁止win10易升更新(转)
- Linux: 软件包管理之rpm与yum [转]
- day21:包和异常处理
- [leetcode]Largest Rectangle in Histogram @ Python