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

时间:2017-07-23 11:56:26

标签: c# asp.net .net-core

所以我希望能够从终端运行dotnet(.net核心mvc项目)时选择我的环境。我发现this post并且认为第二个最高答案是解决它的简洁方法,简而言之:

用以下内容替换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();
}

然后跑步:

dotnet run environment=development
dotnet run environment=staging

所以我粘贴了它,它说我需要添加一个使用状态

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?)

我可能会遇到什么问题。我的意思是here's AddCommandLine(IConfigurationBuilder, String[]) 与名称空间Microsoft.Extensions.Configuration的定义?

1 个答案:

答案 0 :(得分:5)

虽然名称空间Microsoft.Extensions.Configuration,但扩展名在Microsoft.Extensions.Configuration.CommandLine程序集中,位于Microsoft.Extensions.Configuration.CommandLine package。您需要在该包上添加依赖项。