Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Flow hosting configuration through to application #488

Closed
@DamianEdwards

Description

@DamianEdwards

There's currently no way to get access to the configuration constructed and used by the hosting layer, from the application. This is useful in cases where the application wants to get access to values that have already been parsed into an IConfiguration by the hosting layer, e.g. command line arguments.

I propose we add a new property to IHostingEnvironment: IConifguration Configuration { get; }

This would be set by WebHostBuilder when it's initializing the IHostingEnvironment instance (e.g. somewhere around here).

That way, an application can simply accept IHostingEnvironment in its constructor to get access to the hosting's configuration.

Example usage:

public class Startup
{
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);

    public Startup(IHostingEnvironment hosting)
    {

        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables()
            .AddConfiguration(hosting.Configuration);

        Configuration = builder.Build();

        Configuration.Bind(Options);
    }

    public IConfigurationRoot Configuration { get; set; }

    public StartupOptions Options { get; } = new StartupOptions();

    public class StartupOptions
    {
        public bool EnableThing { get; set; }
    }
        ...

Required change to configuration to support this aspnet/Configuration#339

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions