找不到AddCommandLine .netcore 2.1

leo*_*eox 2 .net-standard-2.0 asp.net-core-2.1

当我试图在ConfigurationBuilder上调用AddCommandLine()函数时,我收到指定的语法错误

  • IConfigurationBuilder没有定义或不包含扩展方法AddCommandLine接受第一个参数为IConfigurationBuilder

项目是.NetStandard 2.0

环境:

  • .net核心2.1
  • .net标准2.0
  • 操作系统:窗口10

代码段:

var config = new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddCommandLine(args)
            .Build();
Run Code Online (Sandbox Code Playgroud)

注意:尝试使用Microsoft.Extensions.Configuration中的ConfigurationBuilder

sto*_*ild 9

收到以下错误时:

'IConfigurationBuilder' does not contain a definition for 'AddCommandLine' and no extension method 'AddCommandLine' accepting a first argument of type 'IConfigurationBuilder'
Run Code Online (Sandbox Code Playgroud)

在Visual Studio中的Nuget包管理器控制台上运行以下命令

Install-Package -ProjectName MyProj Microsoft.Extensions.Configuration.CommandLine
Run Code Online (Sandbox Code Playgroud)

或者使用dotnet cli运行以下命令:

dotnet add myproj.csproj package Microsoft.Extensions.Configuration.CommandLine
Run Code Online (Sandbox Code Playgroud)

使用GenericHost时,各种扩展的参考错误可能很常见

使用GenericHost时,可以选择添加元数据包,例如:

用于ASP.NET Core 2.1的Microsoft.AspNetCore.App元数据包

记录原始答案柯克·拉金