

ASP.NET Core 2.0 Configuration Changes
source link: https://joonasw.net/view/aspnet-core-2-configuration-changes
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In the previous article we had a look at Azure AD Authentication in ASP.NET Core 2.0.
This time, we will look at what has changed in configuration.
Configuration building moved to Program.cs
A major change is that instead of building the configuration in Startup
, we do it in the Program
class.
Here is a simple example:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost().Run();
}
public static IWebHost BuildWebHost()
{
return new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((builderContext, config) =>
{
IHostingEnvironment env = builderContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
})
.UseStartup<Startup>()
.Build();
}
}
Take note of the ConfigureAppConfiguration
function call:
.ConfigureAppConfiguration((builderContext, config) =>
{
IHostingEnvironment env = builderContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
The function takes a single argument, which is a callback that takes a WebHostBuilderContext
and an IConfigurationBuilder
.
You can configure configuration as usual in there.
But wait! How can we access the built configuration in Startup
?
The new Startup
constructor looks like this:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
Quite neat right? Now the Startup
class is not defining its configuration, which means we can use alternative configurations very easily.
You don't need to inject the configuration to Startup if you don't use it there somehow. Before you might have added it to the service collection to inject it into other components. It is now done for you by WebHostBuilder
, so that has become unnecessary.
WebHost.CreateDefaultBuilder
ASP.NET Core 2.0 offers a nice convenience method for creating a very typical configuration for an application.
Instead of writing this:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
return new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
if (env.IsDevelopment())
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
}
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
})
.UseStartup<Startup>()
.Build();
}
}
You can just write:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
You can check the source code for this convenience method here.
So what does it do for configuration?
It registers the following configuration sources:
- appsettings.json
- appsettings.environment.json (e.g. appsettings.Development.json)
- User secrets for the current assembly (if in Development environment)
- Environment variables
- Command-line arguments (if not null)
This makes it quite a lot easier to create a typical configuration for an ASP.NET Core app.
Do note you can call ConfigureAppConfiguration
again after creating the default builder. Doing so will not replace the default configuration, but add on top of it.
E.g.:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true);
})
.UseStartup<Startup>()
.Build();
Thanks for reading! Hopefully this article helps you, and don't hesitate to leave a comment if you have questions.
Related links
Recommend
-
98
ASP.NET MVC 中的过滤器(Filter)是 AOP(面向切面编程) 思想的一种实现,供我们在执行管道的特定阶段执行代码,通过使用过滤器可以实现 短路请求、缓存请求结果、日志统一记录、参数合法性验证、异常统一处理、返回值格式化 等等,同时...
-
107
连发了几篇ASP.NETCore文章,果不其然接到各方询问:「喵的妈呀,微软又推新东西了?」「WebForm玩完了吗?」「我ASP.NETMVC还没开始玩耶,是不是不用学了?」先简单答复以上疑问:是的,ASP.NETCore 是下一代的ASP.NET,能跨平台执行,预期是
-
60
NHibernate has been my favorite ORM for long time. Although it’s more complex for beginners than Entity Framework it’s more matured and many developers consider it to be practially an industry standard. NHibernate works w...
-
7
ASP.NET Core 是一个开源的,跨平台的,精简的模块化框架,可用于构建高性能,可扩展的web应用程序, ASP.NET Core 中的数据配置常用 k-v 的形式存储,值得注意的是,新的数据配置还支持 层级方式 ,在这篇文章中,我们将会...
-
16
Debugging configuration values in ASP.NET Core Recently I saw this tweet from Cecil Philip: IConfigurationRoot.GetDebugView is really useful for insp...
-
11
In a previous post I showed how you could create your own version of
-
5
ASP.NET Core Web API 教程 本系列文章主要参考了《Ultimate ASP.NET Core 3 Web API》一书,我对原文进行了翻译,同时适当删减、修改了一部分内容。 对于某些概念和原理,原书和本文中都没有进行详细描述,如果一一详细介绍,内...
-
10
注:本文隶属于《理解ASP.NET Core》系列文章,请查看置顶博客或点击此处查看全文目录 配置提供程序 在.NET中,配置是通过多种配置提供...
-
7
ASP.NET Core 1.0 Configuration Deep Dive Posted on: 29-06-2016
-
3
ASP.NET SPA Templates Proxy Changes From .NET 5 to .NET 6, .NET 7, and On May 04, 2023...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK