Hosting and startup infrastructures for applications.
$ dotnet add package Microsoft.Extensions.HostingContains the .NET Generic Host HostBuilder which layers on the Microsoft.Extensions.Hosting.Abstractions package.
HostBuilder.For a console app project:
using (IHost host = new HostBuilder().Build())
{
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
lifetime.ApplicationStarted.Register(() =>
{
Console.WriteLine("Started");
});
lifetime.ApplicationStopping.Register(() =>
{
Console.WriteLine("Stopping firing");
Console.WriteLine("Stopping end");
});
lifetime.ApplicationStopped.Register(() =>
{
Console.WriteLine("Stopped firing");
Console.WriteLine("Stopped end");
});
host.Start();
// Listens for Ctrl+C.
host.WaitForShutdown();
}
The main types provided by this library are:
Microsoft.Extensions.Host.Microsoft.Extensions.Hosting.HostApplicationBuilderMicrosoft.Extensions.Hosting.HostBuilderMicrosoft.Extensions.Hosting.IHostedServiceMicrosoft.Extensions.Hosting.IHostedLifecycleServiceMicrosoft.Extensions.ConfigurationMicrosoft.Extensions.DependencyInjectionMicrosoft.Extensions.Hosting.AbstractionsMicrosoft.Extensions.LoggingMicrosoft.Extensions.OptionsMicrosoft.Extensions.Hosting is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.