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

bar*_*e.m 14 c# asp.net asp.net-core

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

'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>();
    }
Run Code Online (Sandbox Code Playgroud)

看看这个例子,我发现我没有明显的错误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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

mas*_*son 38

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

using Microsoft.Extensions.Configuration;
Run Code Online (Sandbox Code Playgroud)

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


Car*_*rlo 23

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


spa*_*ead 6

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