Description
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:
- Use a custom configuration value from a source that can be set easily from the same context in which the tool is run, and condition code in the application based on that value, e.g. an environment variable, and then ensure that when the tool is run that configuration value is set.
- A variation of this pattern is to use the existing environment variable for setting the
IHostingEnvironment.EnvironmentName
property and then check that viaIHostingEnvironment.IsEnvironment(string environmentName)
, example
- A variation of this pattern is to use the existing environment variable for setting the
- Check the name of the type implementing
IServer
and/orIHostApplicationLifetime
to see if it matches the name of known private types that tools inject - Check the application's parent process name to see if it's something other than that expected when the application is hosted normally
- EF Core actually sets a flag that can be used by apps to detect when they're being run for the purposes of design-time discovery
Proposal
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.
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; }
}
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:
- Passing a known named flag via the application's entry point
args
parameter- This could be problematic as args are often parsed and validated by applications which then fail if an unexpected arg is passed
- Setting a known named environment variable before the application's entry point is invoked
- Not sure that all tools actually start a separate process to host the application which might make it difficult to set an process-scoped environment variable