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

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.

主站蜘蛛池模板: 荆门市| 阳信县| 四平市| 乐山市| 永济市| 博野县| 敖汉旗| 南和县| 富锦市| 开原市| 诏安县| 上饶县| 申扎县| 湄潭县| 沙田区| 余干县| 通渭县| 凤凰县| 铜鼓县| 古交市| 泸水县| 曲阜市| 盘山县| 班戈县| 金湖县| 柘城县| 克拉玛依市| 吉水县| 贡山| 宁安市| 南靖县| 济南市| 龙口市| 上思县| 松江区| 河津市| 潞城市| 化隆| 宜宾县| 吴堡县| 永宁县|