官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 青铜峡市| 绥德县| 右玉县| 鹤峰县| 道孚县| 察隅县| 聊城市| 英德市| 鸡泽县| 富锦市| 洛川县| 元阳县| 镇坪县| 湟中县| 西安市| 喜德县| 顺平县| 鹤峰县| 天气| 邵阳市| 平湖市| 新闻| 百色市| 龙岩市| 元江| 长岭县| 永宁县| 宝应县| 开原市| 三门县| 宕昌县| 镇康县| 江北区| 永定县| 林甸县| SHOW| 博野县| 乡城县| 新化县| 芒康县| 凤山县|