- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 498字
- 2021-06-18 18:35:54
Understanding the project templates
The Visual Studio template for creating an ASP.NET Core project, since version 3.x, adds the following (or very similar) contents to the Program class:
publicstaticvoid Main(string[] args) { CreateHostBuilder(args).Build().Run(); } publicstatic IHostBuilder CreateHostBuilder(string[] args) => Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
});
This has changed a bit since previous versions and is now more opinionated; I already showed this when talking about OWIN earlier in this chapter.
The Host class exposes the static CreateDefaultBuilder, which returns a fully built IHostBuilder instance. The CreateDefaultBuilder method is actually doing a lot of things behind our backs:
- Creates a ConfigurationBuilder and adds the environment variables provider to it (see Chapter 2, Configuration, for more details)
- Adds the appsettings.json (mandatory) and appsettings.<environment>.json (optional) JSON files and provider to the configuration builder
- Configures the user secrets configuration, if running in development mode
- Configures command-line configuration, if command-line arguments were passed
- Sets Kestrel as the host to use and loads Kestrel-related configurations
- Sets the content root to be the current directory
- Sets the host to use the URLs passed as the ASPNETCORE_SERVER.URLSenvironment variable, if it exists
- Configures logging to the console, debug, EventSource, and EventLog (if in Windows)
- Adds IIS integration
- Sets the default host lifetime as ConsoleHostLifetime
- Configures service provider parameters to validate the scope of registered services and lifetimes if running in the Development environment
- Registers some services, such as IConfiguration
These are the defaults you get, but you can override any of them by using some extension methods over the IHostBuilder interface:
Host
.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
//add or remove from the configuration builder
})
.ConfigureContainer<MyContainer>((context, container) =>
{
//configure container
})
.ConfigureLogging((context, builder) =>
{
//add or remove from the logging builder
});
.ConfigureServices(services =>
{
//register services
})
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
});
After the default builder is instantiated, we ask it to use the Startup class, which is where we can configure the exact stuff we want, such as registered services, middleware components, and so on
IHostBuilder then builds an IHost and then we ask it to run. This is what actually gets our application working.
We have talked about the Startup class before. Basically, it exposes two methods, named ConfigureServices and Configureby convention; the first is used to register services and their implementations with the default DI provider (and possibly use a different one), and the second one is used to add middleware components to the ASP.NET Core pipeline.
The main things you need to remember here are as follows:
- Kestrel is the default host server.
- Configuration providers for JSON and the environment are added automatically; user secrets are added if running in Development environment. There should be one appsettings.json file and possibly one appsettings.<environment>.json file, with overrides per environment.
- Logging is enabled for the console and debug pane of Visual Studio.
Now that we have looked at these templates, let's see what has changed since version 2.0 and how the different tools, templates, features, and so on are affected by it.
- Mastering ServiceStack
- Rake Task Management Essentials
- C語言程序設(shè)計案例式教程
- WordPress Plugin Development Cookbook(Second Edition)
- C語言從入門到精通
- Orleans:構(gòu)建高性能分布式Actor服務(wù)
- .NET Standard 2.0 Cookbook
- Troubleshooting Citrix XenApp?
- 微信小程序開發(fā)實戰(zhàn):設(shè)計·運營·變現(xiàn)(圖解案例版)
- ActionScript 3.0從入門到精通(視頻實戰(zhàn)版)
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- 從零開始學(xué)Python大數(shù)據(jù)與量化交易
- Java EE輕量級解決方案:S2SH
- CryENGINE Game Programming with C++,C#,and Lua
- Pandas入門與實戰(zhàn)應(yīng)用:基于Python的數(shù)據(jù)分析與處理