--- title: .NET Generic Host author: guardrex description: Learn about ASP.NET Core's Generic Host, which is responsible for app startup and lifetime management. monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc ms.date: 11/28/2018 uid: fundamentals/host/generic-host --- # .NET Generic Host By [Luke Latham](https://github.com/guardrex) ::: moniker range="<= aspnetcore-2.2" ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management. This article covers the ASP.NET Core Generic Host (), which is used for apps that don't process HTTP requests. The purpose of Generic Host is to decouple the HTTP pipeline from the Web Host API to enable a wider array of host scenarios. Messaging, background tasks, and other non-HTTP workloads based on Generic Host benefit from cross-cutting capabilities, such as configuration, dependency injection (DI), and logging. Generic Host is new in ASP.NET Core 2.1 and isn't suitable for web hosting scenarios. For web hosting scenarios, use the [Web Host](xref:fundamentals/host/web-host). Generic Host will replace Web Host in a future release and act as the primary host API in both HTTP and non-HTTP scenarios. ::: moniker-end ::: moniker range="> aspnetcore-2.2" ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management. This article covers the .NET Core Generic Host (). Generic Host differs from Web Host in that it decouples the HTTP pipeline from the Web Host API to enable a wider array of host scenarios. Messaging, background tasks, and other non-HTTP workloads can use Generic Host and benefit from cross-cutting capabilities, such as configuration, dependency injection (DI), and logging. Starting in ASP.NET Core 3.0, Generic Host is recommended for both HTTP and non-HTTP workloads. An HTTP server implementation, if included, runs as an implementation of . `IHostedService` is an interface that can be used for other workloads as well. Web Host is no longer recommended for web apps but remains available for backward compatibility. > [!NOTE] > This remainder of this article has not yet been updated for 3.0. ::: moniker-end [View or download sample code](https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals/host/generic-host/samples/) ([how to download](xref:index#how-to-download-a-sample)) When running the sample app in [Visual Studio Code](https://code.visualstudio.com/), use an *external or integrated terminal*. Don't run the sample in an `internalConsole`. To set the console in Visual Studio Code: 1. Open the *.vscode/launch.json* file. 1. In the **.NET Core Launch (console)** configuration, locate the **console** entry. Set the value to either `externalTerminal` or `integratedTerminal`. ## Introduction The Generic Host library is available in the namespace and provided by the [Microsoft.Extensions.Hosting](https://www.nuget.org/packages/Microsoft.Extensions.Hosting/) package. The `Microsoft.Extensions.Hosting` package is included in the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app) (ASP.NET Core 2.1 or later). is the entry point to code execution. Each `IHostedService` implementation is executed in the order of [service registration in ConfigureServices](#configureservices). is called on each `IHostedService` when the host starts, and is called in reverse registration order when the host shuts down gracefully. ## Set up a host is the main component that libraries and apps use to initialize, build, and run the host: [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_HostBuilder)] ## Options configure options for the . ### Shutdown timeout sets the timeout for . The default value is five seconds. The following option configuration in `Program.Main` increases the default five second shutdown timeout to 20 seconds: ```csharp var host = new HostBuilder() .ConfigureServices((hostContext, services) => { services.Configure(option => { option.ShutdownTimeout = System.TimeSpan.FromSeconds(20); }); }) .Build(); ``` ## Default services The following services are registered during host initialization: * [Environment](xref:fundamentals/environments) () * * [Configuration](xref:fundamentals/configuration/index) () * () * () * * [Options](xref:fundamentals/configuration/options) () * [Logging](xref:fundamentals/logging/index) () ## Host configuration Host configuration is created by: * Calling extension methods on to set the [content root](#content-root) and [environment](#environment). * Reading configuration from configuration providers in . ### Extension methods ### Application key (name) The [IHostingEnvironment.ApplicationName](xref:Microsoft.Extensions.Hosting.IHostingEnvironment.ApplicationName*) property is set from host configuration during host construction. To set the value explicitly, use the [HostDefaults.ApplicationKey](xref:Microsoft.Extensions.Hosting.HostDefaults.ApplicationKey): **Key**: applicationName **Type**: *string* **Default**: The name of the assembly containing the app's entry point. **Set using**: `HostBuilderContext.HostingEnvironment.ApplicationName` **Environment variable**: `APPLICATIONNAME` (`` is [optional and user-defined](#configurehostconfiguration)) ### Content root This setting determines where the host begins searching for content files. **Key**: contentRoot **Type**: *string* **Default**: Defaults to the folder where the app assembly resides. **Set using**: `UseContentRoot` **Environment variable**: `CONTENTROOT` (`` is [optional and user-defined](#configurehostconfiguration)) If the path doesn't exist, the host fails to start. [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_UseContentRoot)] ### Environment Sets the app's [environment](xref:fundamentals/environments). **Key**: environment **Type**: *string* **Default**: Production **Set using**: `UseEnvironment` **Environment variable**: `ENVIRONMENT` (`` is [optional and user-defined](#configurehostconfiguration)) The environment can be set to any value. Framework-defined values include `Development`, `Staging`, and `Production`. Values aren't case sensitive. [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_UseEnvironment)] ### ConfigureHostConfiguration `ConfigureHostConfiguration` uses an to create an for the host. The host configuration is used to initialize the for use in the app's build process. `ConfigureHostConfiguration` can be called multiple times with additive results. The host uses whichever option sets a value last on a given key. Host configuration automatically flows to app configuration ([ConfigureAppConfiguration](#configureappconfiguration) and the rest of the app). No providers are included by default. You must explicitly specify whatever configuration providers the app requires in `ConfigureHostConfiguration`, including: * File configuration (for example, from a *hostsettings.json* file). * Environment variable configuration. * Command-line argument configuration. * Any other required configuration providers. File configuration of the host is enabled by specifying the app's base path with `SetBasePath` followed by a call to one of the [file configuration providers](xref:fundamentals/configuration/index#file-configuration-provider). The sample app uses a JSON file, *hostsettings.json*, and calls to consume the file's host configuration settings. To add [environment variable configuration](xref:fundamentals/configuration/index#environment-variables-configuration-provider) of the host, call on the host builder. `AddEnvironmentVariables` accepts an optional user-defined prefix. The sample app uses a prefix of `PREFIX_`. The prefix is removed when the environment variables are read. When the sample app's host is configured, the environment variable value for `PREFIX_ENVIRONMENT` becomes the host configuration value for the `environment` key. During development when using [Visual Studio](https://www.visualstudio.com/) or running an app with `dotnet run`, environment variables may be set in the *Properties/launchSettings.json* file. In [Visual Studio Code](https://code.visualstudio.com/), environment variables may be set in the *.vscode/launch.json* file during development. For more information, see . [Command-line configuration](xref:fundamentals/configuration/index#command-line-configuration-provider) is added by calling . Command-line configuration is added last to permit command-line arguments to override configuration provided by the earlier configuration providers. *hostsettings.json*: [!code-csharp[](generic-host/samples/2.x/GenericHostSample/hostsettings.json)] Additional configuration can be provided with the [applicationName](#application-key-name) and [contentRoot](#content-root) keys. Example `HostBuilder` configuration using `ConfigureHostConfiguration`: [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_ConfigureHostConfiguration)] ## ConfigureAppConfiguration App configuration is created by calling on the implementation. `ConfigureAppConfiguration` uses an to create an for the app. `ConfigureAppConfiguration` can be called multiple times with additive results. The app uses whichever option sets a value last on a given key. The configuration created by `ConfigureAppConfiguration` is available at [HostBuilderContext.Configuration](xref:Microsoft.Extensions.Hosting.HostBuilderContext.Configuration*) for subsequent operations and in . App configuration automatically receives host configuration provided by [ConfigureHostConfiguration](#configurehostconfiguration). Example app configuration using `ConfigureAppConfiguration`: [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_ConfigureAppConfiguration)] *appsettings.json*: [!code-csharp[](generic-host/samples/2.x/GenericHostSample/appsettings.json)] *appsettings.Development.json*: [!code-csharp[](generic-host/samples/2.x/GenericHostSample/appsettings.Development.json)] *appsettings.Production.json*: [!code-csharp[](generic-host/samples/2.x/GenericHostSample/appsettings.Production.json)] To move settings files to the output directory, specify the settings files as [MSBuild project items](/visualstudio/msbuild/common-msbuild-project-items) in the project file. The sample app moves its JSON app settings files and *hostsettings.json* with the following `` item: ```xml ``` ## ConfigureServices adds services to the app's [dependency injection](xref:fundamentals/dependency-injection) container. `ConfigureServices` can be called multiple times with additive results. A hosted service is a class with background task logic that implements the interface. For more information, see . The [sample app](https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals/host/generic-host/samples/) uses the `AddHostedService` extension method to add a service for lifetime events, `LifetimeEventsHostedService`, and a timed background task, `TimedHostedService`, to the app: [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_ConfigureServices)] ## ConfigureLogging adds a delegate for configuring the provided . `ConfigureLogging` may be called multiple times with additive results. [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_ConfigureLogging)] ### UseConsoleLifetime listens for `Ctrl+C`/SIGINT or SIGTERM and calls to start the shutdown process. `UseConsoleLifetime` unblocks extensions such as [RunAsync](#runasync) and [WaitForShutdownAsync](#waitforshutdownasync). is pre-registered as the default lifetime implementation. The last lifetime registered is used. [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_UseConsoleLifetime)] ## Container configuration To support plugging in other containers, the host can accept an . Providing a factory isn't part of the DI container registration but is instead a host intrinsic used to create the concrete DI container. [UseServiceProviderFactory(IServiceProviderFactory<TContainerBuilder>)](xref:Microsoft.Extensions.Hosting.HostBuilder.UseServiceProviderFactory*) overrides the default factory used to create the app's service provider. Custom container configuration is managed by the method. `ConfigureContainer` provides a strongly-typed experience for configuring the container on top of the underlying host API. `ConfigureContainer` can be called multiple times with additive results. Create a service container for the app: [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/ServiceContainer.cs)] Provide a service container factory: [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/ServiceContainerFactory.cs)] Use the factory and configure the custom service container for the app: [!code-csharp[](generic-host/samples-snapshot/2.x/GenericHostSample/Program.cs?name=snippet_ContainerConfiguration)] ## Extensibility Host extensibility is performed with extension methods on `IHostBuilder`. The following example shows how an extension method extends an `IHostBuilder` implementation with the [TimedHostedService](xref:fundamentals/host/hosted-services#timed-background-tasks) example demonstrated in . ```csharp var host = new HostBuilder() .UseHostedService() .Build(); await host.StartAsync(); ``` An app establishes the `UseHostedService` extension method to register the hosted service passed in `T`: ```csharp using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; public static class Extensions { public static IHostBuilder UseHostedService(this IHostBuilder hostBuilder) where T : class, IHostedService, IDisposable { return hostBuilder.ConfigureServices(services => services.AddHostedService()); } } ``` ## Manage the host The implementation is responsible for starting and stopping the `IHostedService` implementations that are registered in the service container. ### Run runs the app and blocks the calling thread until the host is shut down: ```csharp public class Program { public void Main(string[] args) { var host = new HostBuilder() .Build(); host.Run(); } } ``` ### RunAsync runs the app and returns a `Task` that completes when the cancellation token or shutdown is triggered: ```csharp public class Program { public static async Task Main(string[] args) { var host = new HostBuilder() .Build(); await host.RunAsync(); } } ``` ### RunConsoleAsync enables console support, builds and starts the host, and waits for `Ctrl+C`/SIGINT or SIGTERM to shut down. ```csharp public class Program { public static async Task Main(string[] args) { var hostBuilder = new HostBuilder(); await hostBuilder.RunConsoleAsync(); } } ``` ### Start and StopAsync starts the host synchronously. attempts to stop the host within the provided timeout. ```csharp public class Program { public static async Task Main(string[] args) { var host = new HostBuilder() .Build(); using (host) { host.Start(); await host.StopAsync(TimeSpan.FromSeconds(5)); } } } ``` ### StartAsync and StopAsync starts the app. stops the app. ```csharp public class Program { public static async Task Main(string[] args) { var host = new HostBuilder() .Build(); using (host) { await host.StartAsync(); await host.StopAsync(); } } } ``` ### WaitForShutdown is triggered via the , such as (listens for `Ctrl+C`/SIGINT or SIGTERM). `WaitForShutdown` calls . ```csharp public class Program { public void Main(string[] args) { var host = new HostBuilder() .Build(); using (host) { host.Start(); host.WaitForShutdown(); } } } ``` ### WaitForShutdownAsync returns a `Task` that completes when shutdown is triggered via the given token and calls . ```csharp public class Program { public static async Task Main(string[] args) { var host = new HostBuilder() .Build(); using (host) { await host.StartAsync(); await host.WaitForShutdownAsync(); } } } ``` ### External control External control of the host can be achieved using methods that can be called externally: ```csharp public class Program { private IHost _host; public Program() { _host = new HostBuilder() .Build(); } public async Task StartAsync() { _host.StartAsync(); } public async Task StopAsync() { using (_host) { await _host.StopAsync(TimeSpan.FromSeconds(5)); } } } ``` is called at the start of , which waits until it's complete before continuing. This can be used to delay startup until signaled by an external event. ## IHostingEnvironment interface provides information about the app's hosting environment. Use [constructor injection](xref:fundamentals/dependency-injection) to obtain the `IHostingEnvironment` in order to use its properties and extension methods: ```csharp public class MyClass { private readonly IHostingEnvironment _env; public MyClass(IHostingEnvironment env) { _env = env; } public void DoSomething() { var environmentName = _env.EnvironmentName; } } ``` For more information, see . ## IApplicationLifetime interface allows for post-startup and shutdown activities, including graceful shutdown requests. Three properties on the interface are cancellation tokens used to register `Action` methods that define startup and shutdown events. | Cancellation Token | Triggered when… | | ------------------ | --------------------- | | | The host has fully started. | | | The host is completing a graceful shutdown. All requests should be processed. Shutdown blocks until this event completes. | | | The host is performing a graceful shutdown. Requests may still be processing. Shutdown blocks until this event completes. | Constructor-inject the `IApplicationLifetime` service into any class. The [sample app](https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals/host/generic-host/samples/) uses constructor injection into a `LifetimeEventsHostedService` class (an `IHostedService` implementation) to register the events. *LifetimeEventsHostedService.cs*: [!code-csharp[](generic-host/samples/2.x/GenericHostSample/LifetimeEventsHostedService.cs?name=snippet1)] requests termination of the app. The following class uses `StopApplication` to gracefully shut down an app when the class's `Shutdown` method is called: ```csharp public class MyClass { private readonly IApplicationLifetime _appLifetime; public MyClass(IApplicationLifetime appLifetime) { _appLifetime = appLifetime; } public void Shutdown() { _appLifetime.StopApplication(); } } ``` ## Additional resources * * [Hosting repo samples on GitHub](https://github.com/aspnet/Hosting/tree/release/2.1/samples)