The goal of this project is to document the pattern of using standard libraries to setup a "host" for an applications. An application can be a console app, a desktop app (WinForm, WPF), or a web app (MVC, Blazor). These libraries are available for both .NET and .NET Framework runtimes and so the design pattern is mostly the same when developing applications in either runtime.
First step is to create a "builder" for the following tasks:
- Read configurations from setting files, environment variables, and/or command line arguments
- Build-up the dependency injection container to inlcude objects from standard framework libraries (MVC, Razor Page, WebAPI, Blazor, etc.) and from your custom libraries.
- Add logging providers (console, file, etc.)
- Configure Kestrel server and/or IIS integration for web application
Next, create the "app/host" from the "builder" to "run" your app. For web application, there will be addtional tasks to setup your app:
- Add middlewares from standard framework libraries (MVC, Razor Page, WebAPI, Blazor, etc.)
- Add authorization and authentication (OAuth, cookies, etc.)
- Add routing endpoints
There are many options to create a "builder" for different types of application. Some builders include all the basic configuration tasks. Others allow each task to be specified/overrided.
- WebApplicationBuilder
- HostApplicationBuilder
- WebHostBuilder
- IHostBuilder
// class : WebAppSetup.cs, HostSetup.cs
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
app.Run();// class : CommonHostSetup.cs
HostApplicationBuilder builder = Host.CreateApplicationBuilder(settings);
IHost host = builder.Build();
host.Run();// class : CommonHostSetup.cs
IWebHostBuilder builder = new WebHostBuilder();
IWebHost host = builder.Build();
host.Run();// class : CommonHostSetup.cs
IHostBuilder builder = Host.CreateDefaultBuilder(args);
IHost host = builder.Build();
host.Run();These assemblies are available for various .NET versions, .NET Standard, and .NET Framework.
- Microsoft.AspNetCore
- Microsoft.AspNetCore.Hosting
- Microsoft.AspNetCore.Hosting.Abstraction
- Microsoft.AspNetCore.Http.Abstractions
- Microsoft.AspNetCore.Routing
- Microsoft.AspNetCore.Server.Kestrel
- Microsoft.Extension.Hosting
- Microsoft.Extensions.Hosting.Abstractions
- Microsoft.Extensions.Logging.Console
- Microsoft.Extensions.Logging.EventLog
- Microsoft.Extensions.Logging.EventSource
- Microsoft.Extensions.Logging.TraceSource
- Microsoft.Extensions.Logging.Configuration
- Microsoft.Extensions.Configuration.CommandLine
- Microsoft.Extensions.Configuration.Ini
- Microsoft.Extensions.Configuration.EnvironmentVariables
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.Configuration.KeyPerFile
- Microsoft.Extensions.Configuration.UserSecrets
- Microsoft.Extensions.Configuration.Xml