Skip to content

Provide API in Microsoft.Extensions.Hosting to allow apps to know when they're being run in the context of a tool #85739

Open
@DamianEdwards

Description

@DamianEdwards

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 registered DbContexts 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-op IServer and IHostApplicationLifetime such that details of the app's endpoints can be obtained for the purposes of generating OpenAPI documents.
  • Swashbuckle.AspNetCore.Cli: Functionally similar to Microsoft.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 via IHostingEnvironment.IsEnvironment(string environmentName), example
  • Check the name of the type implementing IServer and/or IHostApplicationLifetime 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-Hostingdesign-discussionOngoing discussion about design without consensus

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions