我正在尝试使用dotnet build
从CLI构建我的.NET Core应用程序,但每次都会出现此错误:
'IConfigurationBuilder'不包含'AddEnvironmentVariables'的定义,并且没有可以找到接受类型'IConfigurationBuilder'的第一个参数的扩展方法'AddEnvironmentVariables'(你是否缺少using指令或汇编引用?)
这是问题发生在ConfigureServices
Startup.cs
中的 public void ConfigureServices(IServiceCollection services)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddEnvironmentVariables()
.Build();
services.AddEntityFrameworkSqlServer()
.AddDbContext<MyContext>(options =>
options.UseSqlServer(builder["Data:MyContext:ConnectionString"]));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<MyContext>()
.AddDefaultTokenProviders()
.AddOpenIddictCore<Application>(config => config.UseEntityFramework());
services.AddMvc();
services.AddScoped<OpenIddictManager<ApplicationUser, Application>, CustomOpenIddictManager>();
}
方法:
Startup.cs
查看this example我发现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"
]
}
文件:
read.table()
2018年10月更新: 在VS 2017的最新版本中创建新的.NET Core 2.1应用程序时不会发生此问题。自从我提出此问题以来,所有内容都处于长期稳定版本。
答案 0 :(得分:24)
您需要范围内的Microsoft.Extensions.Configuration命名空间来获取该扩展方法。要么完全限定它,要么将其添加到文件的顶部:
using Microsoft.Extensions.Configuration;
您还需要NuGet参考Microsoft.Extensions.Configuration.EnvironmentVariables。
答案 1 :(得分:18)
要添加的正确包。AddEnvironmentVariables()
是Microsoft.Extensions.Configuration.EnvironmentVariables
答案 2 :(得分:5)
我尝试了Microsoft.Extensions.Configuration.EnvironmentVariables,但没有运气。我已经从NuGet获得了Microsoft.Extensions.Configuration。我添加了Microsoft.Extensions.Configuration.UserSecrets 然后它奏效了。