3

So I wanted to be able to choose my environment when running dotnet (an .net core mvc-project) from the terminal. I found this post and thought the second highest answer was a neat way of solving it, in short:

Replacing the Program class body with the following:

private static readonly Dictionary<string, string> defaults =
new Dictionary<string, string> {
    { WebHostDefaults.EnvironmentKey, "development" }
};

public static void Main(string[] args)
{
    var configuration =
        new ConfigurationBuilder()
            .AddInMemoryCollection(defaults)
            .AddEnvironmentVariables("ASPNETCORE_")
            .AddCommandLine(args)
            .Build();

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

Then running:

dotnet run environment=development
dotnet run environment=staging

So I pasted it, and it said I need to add a using statment

using Microsoft.Extensions.Configuration;

Still got this error message:

'IConfigurationBuilder' does not contain a definition for
'AddCommandLine' and no extension method 'AddCommandLine' 
accepting a first argument of type 'IConfigurationBuilder' 
could be found (are you missing a using directive or an 
assembly reference?)

I'm a bit at loss for what could be the problem. I mean, here's the definition of AddCommandLine(IConfigurationBuilder, String[]) with namespace Microsoft.Extensions.Configuration?

MrJalapeno
  • 1,532
  • 3
  • 18
  • 37
  • Which version of `Microsoft.Extensions.Configuration` are you using? – Jon Skeet Jul 23 '17 at 11:59
  • Hmm, first there were red squigglies under `ConfigurationBuilder`, then it just suggested I added `using Microsoft.Extensions.Configuration;` which I did. Didn't think of it, but now that you asked I took a look at my `.deps.json`-file and under `"microsoft.aspnetcore.hosting/1.1.2": { "dependencies": {` there's a line saying `"Microsoft.Extensions.Configuration": "1.1.2",`. So I suppose it's version 1.1.2? – MrJalapeno Jul 23 '17 at 12:06
  • Yes, that's the version number. – Jon Skeet Jul 23 '17 at 12:16
  • Ah, I think I've got it - do you have a dependency on `Microsoft.Extensions.Configuration.CommandLine`? That's where the extension method comes from. – Jon Skeet Jul 23 '17 at 12:18
  • Yes! I ran `dotnet add package Microsoft.Extensions.Configuration.CommandLine` and then all the issues went away. unfortunately it seems it makes little difference whether I run `dotnet run environment=development` or `dotnet run environment=production` or simply `dotnet run`. It seems to be in production mode regardless, though it runs in development when I run it through f5 (as per the instructions of the `launch.json`). So I suppose I learned a way to test both modes, albeit far less elegant. Though I guess that's another question and you've certainly helped me with this one. Thank you! – MrJalapeno Jul 23 '17 at 12:34
  • Just add the answer and I'll accept it :) – MrJalapeno Jul 23 '17 at 12:34
  • Hmm. I'm not sure whether we want to encourage questions which are basically "I haven't checked the dependencies". I'll add it this one time, but please check for dependencies yourself next time. – Jon Skeet Jul 23 '17 at 12:52
  • Ah, ok. Didn't think about that. At least I learned to not only look at the namespace but at the assembly as well, which was new info for me. Thanks! – MrJalapeno Jul 23 '17 at 13:06
  • I actually listed [this package as a comment](https://stackoverflow.com/questions/37322565/dotnet-run-or-dotnet-watch-with-development-environment-from-command-line/39131944?noredirect=1#comment65690597_39131944) to my answer. Vote it up so it is less likely that others miss this in the future. – Technetium Jul 24 '17 at 17:18

1 Answers1

5

Although the namespace is Microsoft.Extensions.Configuration, the extension is in the Microsoft.Extensions.Configuration.CommandLine assembly, which is in the Microsoft.Extensions.Configuration.CommandLine package. You need to add a dependency on that package.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194