由于缺少定义,在.net Core应用程序上构建失败

我正在尝试使用dotnet build从CLI构建我的.NET Core应用程序,但每次我都会收到此错误:

‘IConfigurationBuilder’不包含’AddEnvironmentVariables’的定义,并且没有可以找到接受类型’IConfigurationBuilder’的第一个参数的扩展方法’AddEnvironmentVariables’(你是否缺少using指令或汇编引用?)

这是Startup.cs中我发生问题的ConfigureServices方法:

  public void ConfigureServices(IServiceCollection services) { var builder = new ConfigurationBuilder() .AddJsonFile("config.json") .AddEnvironmentVariables() .Build(); services.AddEntityFrameworkSqlServer() .AddDbContext(options => options.UseSqlServer(builder["Data:MyContext:ConnectionString"])); services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders() .AddOpenIddictCore(config => config.UseEntityFramework()); services.AddMvc(); services.AddScoped<OpenIddictManager, CustomOpenIddictManager>(); } 

看看这个例子,我发现我的Startup.cs没有明显的错误。

更新我的project.json文件:

 { "compilationOptions": { "debugType": "portable", "emitEntryPoint": true, "preserveCompilationContext": true }, "dependencies": { "AspNet.Security.OAuth.Validation": "1.0.0-alpha1-*", "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-*", "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0-rc2-*", "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-*", "Microsoft.AspNetCore.Hosting": "1.0.0-rc2-*", "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-*", "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-rc2-*", "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-*", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-*", "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-*", "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-*", "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-*", "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-*", "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-*", "OpenIddict.Core": "1.0.0-*", "OpenIddict.EF": "1.0.0-*" }, "frameworks": { "net451": { "frameworkAssemblies": { "System.ComponentModel": { "type": "build" } } }, "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0-rc2-*" } }, "imports": [ "dnxcore50", "portable-net451+win8" ] } }, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": { "version": "1.0.0-rc2-*", "imports": "portable-net45+wp80+win8+wpa81+dnxcore50" } }, "scripts": { "postpublish": "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" }, "content": [ "wwwroot", "Views", "config.json", "web.config" ], "exclude": [ "wwwroot", "node_modules" ], "publishExclude": [ "**.user", "**.vspscc" ] } 

2018年10月更新:在VS 2017的最新版本中创建新的.NET Core 2.1应用程序时不会出现此问题。自从我提出这个问题以来,现在一切都处于长期稳定版本。

您需要范围中的Microsoft.Extensions.Configuration命名空间来获取该扩展方法。 要么完全限定它,要么将其添加到文件的顶部:

 using Microsoft.Extensions.Configuration; 

您还需要NuGet参考Microsoft.Extensions.Configuration.EnvironmentVariables 。

要添加的正确包。 AddEnvironmentVariables()Microsoft.Extensions.Configuration.EnvironmentVariables

我尝试了Microsoft.Extensions.Configuration.EnvironmentVariables,但没有运气。 我已经从NuGet获得了Microsoft.Extensions.Configuration。 我添加了Microsoft.Extensions.Configuration.UserSecrets然后它工作。