Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make a function call in in spite of the using-statement and extension needed

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?

like image 909
MrJalapeno Avatar asked Dec 08 '25 13:12

MrJalapeno


1 Answers

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.

like image 150
Jon Skeet Avatar answered Dec 11 '25 02:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!