{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Hosting/issues/820","repository_url":"https://api.github.com/repos/aspnet/Hosting","labels_url":"https://api.github.com/repos/aspnet/Hosting/issues/820/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Hosting/issues/820/comments","events_url":"https://api.github.com/repos/aspnet/Hosting/issues/820/events","html_url":"https://github.com/aspnet/Hosting/issues/820","id":166574886,"number":820,"title":"Passing on IConfiguration to Startup (in order to read environment correctly) ?","user":{"login":"bluemmc","id":12269205,"avatar_url":"https://avatars.githubusercontent.com/u/12269205?v=3","gravatar_id":"","url":"https://api.github.com/users/bluemmc","html_url":"https://github.com/bluemmc","followers_url":"https://api.github.com/users/bluemmc/followers","following_url":"https://api.github.com/users/bluemmc/following{/other_user}","gists_url":"https://api.github.com/users/bluemmc/gists{/gist_id}","starred_url":"https://api.github.com/users/bluemmc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluemmc/subscriptions","organizations_url":"https://api.github.com/users/bluemmc/orgs","repos_url":"https://api.github.com/users/bluemmc/repos","events_url":"https://api.github.com/users/bluemmc/events{/privacy}","received_events_url":"https://api.github.com/users/bluemmc/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/Hosting/labels/needs%20design","name":"needs design","color":"006b75"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-07-20T12:58:17Z","updated_at":"2016-07-30T07:41:26Z","closed_at":null,"body":"I would like to be able to specify the correct runtime environment (test/production etc) from either a command line OR environment variable OR appsettings.json while still allowing for environment specific additional json files.\r\n\r\nTo do so I can do the following in program.cs which is a bit complex. However, once configuration is setup with UseConfiguration in Program.cs there is no way I can pass this configuration the Startup.cs class which only takes a IHostingEnvironment parameter and will not accept a IConfiguration parameter. Instead I have to replicate configuation in Startup but this time I do not have access to the command line \"args\" so I can't make it work. I could use a global variable to temporarily save the  configuration but that would interfere with other usages like testing.... What to do ? \r\n\r\n           // First setup a partial configuration so we can retrieve environment:\r\n            IConfigurationRoot config = new ConfigurationBuilder()\r\n                .AddCommandLine(args)\r\n                .AddEnvironmentVariables()\r\n                .SetBasePath(Directory.GetCurrentDirectory())\r\n                .AddJsonFile(\"appsettings.json\", optional: false)\r\n                .Build();\r\n            string env = config[\"ASPNETCORE_ENVIRONMENT\"];\r\n\r\n            // Next, setup a full configuration using the environment we just found out:\r\n            config = new ConfigurationBuilder()\r\n                .AddCommandLine(args)\r\n                .AddEnvironmentVariables()\r\n                .AddJsonFile(\"appsettings.json\", optional: false)\r\n                .AddJsonFile($\"appsettings.{env}.json\", optional: false)\r\n                .Build();\r\n\r\n            // Create and run hosting\r\n            IWebHost host = new WebHostBuilder()\r\n                .UseKestrel()\r\n                .UseConfiguration(config) // How to access this config in Startup.cs ????\r\n                .UseContentRoot(Directory.GetCurrentDirectory())\r\n                .UseIISIntegration()\r\n                .UseStartup<Startup>()\r\n                .Build();\r\n\r\n            host.Run();\r\n"},"comment":{"url":"https://api.github.com/repos/aspnet/Hosting/issues/comments/236351234","html_url":"https://github.com/aspnet/Hosting/issues/820#issuecomment-236351234","issue_url":"https://api.github.com/repos/aspnet/Hosting/issues/820","id":236351234,"user":{"login":"davidfowl","id":95136,"avatar_url":"https://avatars.githubusercontent.com/u/95136?v=3","gravatar_id":"","url":"https://api.github.com/users/davidfowl","html_url":"https://github.com/davidfowl","followers_url":"https://api.github.com/users/davidfowl/followers","following_url":"https://api.github.com/users/davidfowl/following{/other_user}","gists_url":"https://api.github.com/users/davidfowl/gists{/gist_id}","starred_url":"https://api.github.com/users/davidfowl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidfowl/subscriptions","organizations_url":"https://api.github.com/users/davidfowl/orgs","repos_url":"https://api.github.com/users/davidfowl/repos","events_url":"https://api.github.com/users/davidfowl/events{/privacy}","received_events_url":"https://api.github.com/users/davidfowl/received_events","type":"User","site_admin":false},"created_at":"2016-07-30T07:41:26Z","updated_at":"2016-07-30T07:41:26Z","body":"There's several ways to do this but here's a question, are you trying to unify hosting configuration and application configuration in the same file? Don't think of the UseConfiguration call as something you can access, it's purely for the hosting environment (servers, etc). You should try to configure as much as you can via Startup.\r\n\r\nAs for flowing command line args, here's a pattern we use in our benchmarks repository:\r\n\r\nA service is added to the service collection that flows the command line arguments:\r\n\r\nhttps://github.com/aspnet/benchmarks/blob/dev/src/Benchmarks/Program.cs#L42-L46\r\n\r\nThat'll make it accessible in the `Startup` ctor."}}
{"actor":{"display_login":"davidfowl"}}