当前位置:网站首页>. Dependency injection of NETCORE
. Dependency injection of NETCORE
2022-07-22 12:48:00 【Lava of Rodinia】
//MVC Three built-in dependency injection methods
//MVC Three built-in dependency injection methods
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration, IHostEnvironment hostEnvironment)
{
_logger = logger;
this.configuration = configuration;
this.hostEnvironment = hostEnvironment;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
// The second dependency injection method
IHostEnvironment host = HttpContext.RequestServices.GetService(typeof(IHostEnvironment)) as IHostEnvironment;
// The third way of dependency injection
var host2 = HttpContext.RequestServices.GetRequiredService<IHostEnvironment>();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton<IServiceCollection>();// It's the same one every time
services.AddScoped<ILogger>(); // This time into Controller Of URL Get the same
services.AddTransient<ILogger>(); // Every time it's new
services.AddSingleton<IServiceCollection>(x => new ServiceCollection());
// Equate to
services.AddSingleton<IServiceCollection, ServiceCollection>();
}
边栏推荐
- 等额本金和等额本息还款方式的差异分析
- 直流无刷电机工作原理
- STM32 FSMC使用笔记
- C # split string by string
- Safety system construction - specific operation process of safety equipment monitoring
- Application notes of STM32 FSMC
- 线程池配置 异步编排 CompletableFuture
- [record of question brushing] 17 Letter combination of telephone number
- pip常用命令
- Multifunctional embedded decoding software (1)
猜你喜欢
How to realize the bottom pop-up box encapsulation of wechat applet
Alibaba cloud technology expert Yang Zeqiang: building observable capabilities on elastic computing cloud
There are four ways to get started with precious metals
[PHP environment setup /wamp/ interpreter / download]
Programming skills │ amazing syntax of advanced markdown editor
EasyCVR平台安全扫描提示Go pprof调试信息泄露的解决办法
等额本金和等额本息还款方式的差异分析
C语言指针详解
C# 联合KepServer对PLC进行数据读写
Analysis on the difference between repayment methods of equal principal and equal principal and interest
随机推荐
虚拟DOM是什么
C语言指针详解
[FPGA tutorial case 34] communication case 4 - QPSK modulation signal generation based on FPGA, and its constellation is tested by MATLAB
Misunderstanding of data center construction
Common terms of ad
JS Console Alert REPL 报错 变量 变量声明提升 数据类型 typeof
小程序毕设作品之微信酒店预订小程序毕业设计(1)开发概要
Spark Learning Notes (I) -- simulating distributed computing
Comprendre la technologie de l'adaptateur en NLP
Md5&Md5盐值加密
[record of question brushing] 17 Letter combination of telephone number
Gb/t 30283-2022 information security technology information security service classification and code learning record
[golang | grpc] use grpc to realize the watch function
Network security risk assessment - best case of landing practice in the telecommunications industry
Safety system construction - specific operation process of safety equipment monitoring
121. The best time to buy and sell stocks
Redis core principles
requests库中使用response.json()报错:json.decoder.JSONDecodeError: Expecting value
解决anaconda中执行pip命令显示不是内部或外部命令的方法
Prevention is the best way to solve blackmail software