-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Provide API in Microsoft.Extensions.Hosting to allow apps to know when they're being run in the context of a tool #85739
Comments
Tagging subscribers to this area: @dotnet/area-extensions-hosting Issue DetailsProblemThere are a number of .NET tools & libraries that follow the pattern of loading and executing the application the tool is being invoked in the context of, in order to extract configuration and other details from the application host's DI container, e.g.
A common issue with these tools is that it's difficult to condition code in the application such that it doesn't run when the application is booted in the context of one of these tools. For example, imagine your application has code that executes logic on application start to seed a database with initial data, it's very unlikely that one would want that code to run when the application is run in the context of the tool. Another example relates to validation of application configuration, especially secrets that aren't available in the application source code. In "normal" application startup it's desirable to validate that application is correctly configured with non-null values, but when run in the context of a tool these values aren't required and may even not be available if the tool is being executed as part of a CI configuration. Some approaches that are used in applications today to detect when it's being hosted by a tool:
ProposalProvide an API in Note this is just a starting suggestion intended to help kickstart discussion. Please namespace Microsoft.Extensions.Hosting;
public interface IHostingTool
{
/// <summary>
/// Gets the name of the tool currently hosting the application.
/// </summary>
string? ToolName { get; }
/// <summary>
/// Gets a value that indicates whether the tool will stop the application after the host is built.
/// </summary>
bool StopsApplicationAfterHostBuilt { get; }
}
|
Another potential alternative would be to have well-known I don't think this alternative is better than the current proposal. Just listing it for other ideas. |
@eerhardt the advantages of your proposal as I see it are:
|
The same mechanism it uses today to inject DI services. For example: Instead of calling |
OK so IIUC that means that the configuration value would not be observable in the app until they call the |
Correct - it has the same drawback as the |
Perhaps this is a convenient time to introduce a new event for the |
The issue is the HostFactoryResolver is based on With #85486, we will have an interface that will be able to add to the configuration directly inline. But HostFactoryResolver would need to be modified to work with the new interface. |
Problem
There are a number of .NET tools & libraries that follow the pattern of loading and executing the application the tool is being invoked in the context of, in order to extract configuration and other details from the application host's DI container, e.g.
dotnet ef
: Boots the application to the point of the service container being built so that any registeredDbContext
s can be interacted with to run migrations, generate compiled models, etc.Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>
: Hosts the application for the intent of configuring it and executing requests against it in the context of integration tests.Microsoft.Extensions.ApiDescription.Server
: Includes MSBuild targets and a command line tool that executes the ASP.NET Core application after injecting a no-opIServer
andIHostApplicationLifetime
such that details of the app's endpoints can be obtained for the purposes of generating OpenAPI documents.Swashbuckle.AspNetCore.Cli
: Functionally similar toMicrosoft.Extensions.ApiDescription.Server
but customized for Swashbuckle and designed to be used as a CLI tool directly rather than via MSBuild targets.A common issue with these tools is that it's difficult to condition code in the application such that it doesn't run when the application is booted in the context of one of these tools. For example, imagine your application has code that executes logic on application start to seed a database with initial data, it's very unlikely that one would want that code to run when the application is run in the context of the tool. Another example relates to validation of application configuration, especially secrets that aren't available in the application source code. In "normal" application startup it's desirable to validate that application is correctly configured with non-null values, but when run in the context of a tool these values aren't required and may even not be available if the tool is being executed as part of a CI configuration.
Some approaches that are used in applications today to detect when it's being hosted by a tool:
IHostingEnvironment.EnvironmentName
property and then check that viaIHostingEnvironment.IsEnvironment(string environmentName)
, exampleIServer
and/orIHostApplicationLifetime
to see if it matches the name of known private types that tools injectProposal
Provide an API in
Microsoft.Extensions.Hosting
that would make it easier for an application to detect when it's been loaded in the context of a tool, such that it can perform conditional logic as appropriate. Tools would need to inject/set this API as part of booting the application. If no implementation is registered, the application is not being hosted by a tool. The tools shipped by MS that follow this hosting pattern utilize a shared code package to implement the behavior so implementing this for our own tools would be straightforward.Note this is just a starting suggestion intended to help kickstart discussion.
Note one drawback with this approach is that the app can't evaluate if it's being hosted in the context of a tool until the DI container is built.
Alternatives
Some alternatives and potential drawbacks with them:
args
parameterThe text was updated successfully, but these errors were encountered: