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

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.

主站蜘蛛池模板: 凤冈县| 高唐县| 吉水县| 中宁县| 徐汇区| 霍山县| 芦山县| 登封市| 青岛市| 中阳县| 海伦市| 新乐市| 鞍山市| 呼玛县| 六枝特区| 玉山县| 平凉市| 昂仁县| 咸阳市| 札达县| 胶州市| 鄱阳县| 会宁县| 武隆县| 潞城市| 滁州市| 龙江县| 武功县| 铜鼓县| 读书| 内丘县| 濮阳市| 五莲县| 泾阳县| 宁明县| 来凤县| 荥阳市| 田林县| 常山县| 奎屯市| 砀山县|