Is services.AddSingleton<IConfiguration> really needed in .net core 2 API

岁酱吖の 提交于 2019-12-05 15:19:21

As the official roadmap for ASP.NET Core 2.0 says:

An IConfiguration instance will be added to the services container by default in ASP.NET Core 2.0, so that all applications can easily retrieve configuration values via the container

So services.AddSingleton<IConfiguration> (or similar) is already called by the framework itself.

You may see this behavior inside WebHostBuilder.cs file or (when using the utility extension methods) inside HostBuilder.cs file.

I am not sure if it is needed.But we have appsettings.local.json and if we do not add this in program.cs, we can not read datas from appsetting.local.json. so In program.cs we add ConfigureServices statament in CreateWebHostBuilder method at program.cs

var webHostBuilder = WebHost.CreateDefaultBuilder(args)
                    .UseApplicationInsights()
                    .ConfigureServices(services => services.AddSingleton<IConfiguration>(config))
                    .UseStartup<Startup>();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!