尽管需要使用using语句和扩展,但无法调用函数

MrJ*_*eno 3 c# asp.net .net-core

所以我希望能够dotnet在终端上运行(.net core mvc-project)时选择我的环境.我找到了这篇文章,并认为第二个最高答案是一个简洁的解决方法,简而言之:

用以下内容替换Program类主体:

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();
}
Run Code Online (Sandbox Code Playgroud)

然后运行:

dotnet run environment=development
dotnet run environment=staging
Run Code Online (Sandbox Code Playgroud)

所以我粘贴了它,它说我需要添加一个使用的声明

using Microsoft.Extensions.Configuration;

仍然收到此错误消息:

'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?)
Run Code Online (Sandbox Code Playgroud)

对于可能出现的问题,我有点茫然.我的意思是,AddCommandLine(IConfigurationBuilder, String[]) with namespace 的定义Microsoft.Extensions.Configuration

Jon*_*eet 5

虽然命名空间Microsoft.Extensions.Configuration,但扩展名是在Microsoft.Extensions.Configuration.CommandLine程序Microsoft.Extensions.Configuration.CommandLine中的程序集中.您需要在该包上添加依赖项.