The app uses whichever option sets a value last on a given key. Configuration for subsequent operations and as a service from DI. The host configuration is also added to the app configuration. For more information, see Configuration in ASP. For more information, see the Default builder settings section and Configuration: Environment variables. The IHostEnvironment. ApplicationName property is set from host configuration during host construction. Key : applicationName Type : string Default : The name of the assembly that contains the app's entry point.
ContentRootPath property determines where the host begins searching for content files. If the path doesn't exist, the host fails to start. Key : contentRoot Type : string Default : The folder where the app assembly resides. EnvironmentName property can be set to any value.
Framework-defined values include Development , Staging , and Production. Values aren't case-sensitive. ShutdownTimeout sets the timeout for StopAsync. The default value is five seconds. During the timeout period, the host:.
If the timeout period expires before all of the hosted services stop, any remaining active services are stopped when the app shuts down. The services stop even if they haven't finished processing. If services require more time to stop, increase the timeout. To set this value, use the environment variable or configure HostOptions. The following example sets the timeout to 20 seconds:. By default , appsettings. To disable this reload behavior in ASP.
The colon : separator doesn't work with environment variable hierarchical keys on all platforms. For more information, see Environment variables. Some host settings apply only to HTTP workloads. Extension methods on IWebHostBuilder are available for these settings. Code samples that show how to call the extension methods assume webBuilder is an instance of IWebHostBuilder , as in the following example:. When false , errors during startup result in the host exiting. When true , the host captures exceptions during startup and attempts to start the server.
When enabled, or when the environment is Development , the app captures detailed errors. A semicolon-delimited string of hosting startup assemblies to load on startup. Although the configuration value defaults to an empty string, the hosting startup assemblies always include the app's assembly. When hosting startup assemblies are provided, they're added to the app's assembly for loading when the app builds its common services during startup.
Prevents the automatic loading of hosting startup assemblies, including hosting startup assemblies configured by the app's assembly. For more information, see Use hosting startup assemblies in ASP. To set this value, use the environment variable or call UseStartup. UseStartup can take an assembly name string or a type TStartup.
If multiple UseStartup methods are called, the last one takes precedence. A semicolon-delimited list of IP addresses or host addresses with ports and protocols that the server should listen on for requests. Supported formats vary among servers. Kestrel has its own endpoint configuration API. For more information, see Configure endpoints for the ASP. NET Core Kestrel web server. The IWebHostEnvironment. WebRootPath property determines the relative path to the app's static assets. If the path doesn't exist, a no-op file provider is used.
Key : webroot Type : string Default : The default is wwwroot. Call methods on the built IHost implementation to start and stop the app.
These methods affect all IHostedService implementations that are registered in the service container. Run runs the app and blocks the calling thread until the host is shut down. RunAsync runs the app and returns a Task that completes when the cancellation token or shutdown is triggered.
Start starts the host synchronously. For any future applications, it is recommended to use the. NET Generic Host. In order to get an idea what the above methods do, I looked into the source code on Github. As you can see, it pretty much configures a HostBuilder object and returns it. We will now look through ConfigureWebHostDefaults method.
So far, we have seen that both approaches use the same Generic Host paradigm in the two projects. In both cases, after the configuration sections, we finally call the Run on IHost object implemented in HostingAbstractionsHostExtensions. This will run the app and block the calling thread until the host is shut down. For a worker service, remember how we registered our Worker class by passing it into ConfigureServices method.
So when we run our host, it must be retrieving our Worker service and invoking these methods. Source: Microsoft. In the Host.
So our guess was correct. It certainly invokes the StartAsync method of BackgroundService , that calls ExecuteAsync method in which we have ultimately implemented in our Worker class. A summary of how it reaches this as follows;. The Generic Host can be used with other types of. NET applications, such as Console apps. A host is an object that encapsulates an app's resources and lifetime functionality, such as:.
When a host starts, it calls IHostedService. StartAsync on each implementation of IHostedService registered in the service container's collection of hosted services. ExecuteAsync methods called. The main reason for including all of the app's interdependent resources in one object is lifetime management: control over app startup and graceful shutdown.
This is achieved with the Microsoft. Hosting NuGet package. The host is typically configured, built, and run by code in the Program class. The Main method:. The CreateDefaultBuilder method:.
The ConfigureServices method exposes the ability to add services to the Microsoft. IServiceCollection instance. Later, these services can be made available from dependency injection. Inject the IHostApplicationLifetime service into any class to handle post-startup and graceful shutdown tasks. Three properties on the interface are cancellation tokens used to register app start and app stop event handler methods. The interface also includes a StopApplication method. The IHostLifetime implementation controls when the host starts and when it stops.
The last implementation registered is used. ConsoleLifetime is the default IHostLifetime implementation. For more information on the lifetime mechanics of shutdown, see Host shutdown. Inject the IHostEnvironment service into a class to get information about the following settings:. Host configuration is used to configure properties of the IHostEnvironment implementation.
The host configuration is available in HostBuilderContext. Configuration within the ConfigureAppConfiguration method.
0コメント