尽管需要 using 语句和扩展,但仍无法进行函数调用 [英] Can't make a function call in in spite of the using-statement and extension needed

查看:23
本文介绍了尽管需要 using 语句和扩展,但仍无法进行函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望能够在从终端运行 dotnet(一个 .net 核心 mvc 项目)时选择我的环境.我找到了这篇文章并认为第二高的答案是一种巧妙的解决方法,简而言之:

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:

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

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();
}

然后运行:

dotnet run environment=development
dotnet run environment=staging

所以我粘贴了它,它说我需要添加一个 using 语句

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

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

对于可能是什么问题,我有点不知所措.我的意思是,这里是定义AddCommandLine(IConfigurationBuilder, String[]) 带有命名空间 Microsoft.Extensions.Configuration?

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?

推荐答案

虽然 namespaceMicrosoft.Extensions.Configuration,但扩展在 Microsoft.Extensions.Configuration.CommandLine 程序集,位于 Microsoft.Extensions.Configuration.CommandLine.您需要添加对该包的依赖项.

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.

这篇关于尽管需要 using 语句和扩展,但仍无法进行函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆