构建.NET的核心应用失败由于缺少定义 [英] Build failing on .net Core app due to missing definition

查看:227
本文介绍了构建.NET的核心应用失败由于缺少定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 DOTNET构建来建立从CLI我的.NET的核心应用程序,但每一次我得到这个错误:


  

IConfigurationBuilder'不包含'AddEnvironmentVariables的定义,并没有扩展方法'AddEnvironmentVariables接受型IConfigurationBuilder第一个参数可以找到(是否缺少using指令或程序集引用?)


这是我在 ConfigureServices 方法 Startup.cs 哪里有问题发生:

 公共无效ConfigureServices(IServiceCollection服务)
    {
        VAR建设者=新ConfigurationBuilder()
            .AddJsonFile(config.json)
            .AddEnvironmentVariables()
            。建立();        services.AddEntityFrameworkSqlServer()
             .AddDbContext< MyContext>(选项=>
                 options.UseSqlServer(建设者[数据:MyContext:ConnectionString的]));        services.AddIdentity< ApplicationUser,ApplicationRole>()
            .AddEntityFrameworkStores< MyContext>()
            .AddDefaultTokenProviders()
            .AddOpenIddictCore<应用&GT(配置=> config.UseEntityFramework());        services.AddMvc();        services.AddScoped< OpenIddictManager< ApplicationUser,用途&gt ;, CustomOpenIddictManager>();
    }

看着这个例子我什么也看不到明显的错误与我的 Startup.cs

更新
我的 project.json 文件:

  {
  compilationOptions:{
    DEBUGTYPE:便携,
    emitEntryPoint:真实,
    preserveCompilationContext:真
  },  依赖:{
    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- *
  },  构架: {
    net451:{
      frameworkAssemblies:{
        System.ComponentModel:{类型:建}
      }
    },    netcoreapp1.0:{
      依赖:{
        Microsoft.NETCore.App:{
          类型:平台,
          版本:1.0.0-rc2- *
        }
      },      进口:[
        dnxcore50
        便携式net451 + win8的
      ]
    }
  },  工具:{
    Microsoft.AspNetCore.Server.IISIntegration.Tools:{
      版本:1.0.0-rc2- *,
      进口:便携式net45 + WP80 + win8的+ wpa81 + dnxcore50
    }
  },  脚本:{
    postpublish:DOTNET发布 - iis的--publish文件夹%发布:OutputPath%--framework%发布:FullTargetFramework%
  },  内容:
    wwwroot的
    意见
    config.json
    的web.config
  ]  排除:[
    wwwroot的
    node_modules
  ]  publishExclude:[
    **。用户,
    **。vspscc
  ]
}


解决方案

您需要的<一个href=\"https://github.com/aspnet/Configuration/blob/47b13050126fab7afa18e8636bb3c5d1f4022338/src/Microsoft.Extensions.Configuration.EnvironmentVariables/EnvironmentVariablesExtensions.cs\"相对=nofollow> Microsoft.Extensions.Configuration 范围命名空间得到扩展方法。要么完全限定它,或添加到您的文件的顶部:

 使用Microsoft.Extensions.Configuration;

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

I'm trying to build my .NET Core app from the CLI using dotnet build, but every single time I get this error:

'IConfigurationBuilder' does not contain a definition for 'AddEnvironmentVariables' and no extension method 'AddEnvironmentVariables' accepting a first argument of type 'IConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)

This is my ConfigureServices method in Startup.cs where the problem is happening:

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

Looking at this example I see nothing obviously wrong with my Startup.cs.

Update My project.json file:

{
  "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"
  ]
}

解决方案

You need the Microsoft.Extensions.Configuration namespace in scope to get that extension method. Either fully qualify it, or add this to the top of your file:

using Microsoft.Extensions.Configuration;

You also will need the NuGet reference Microsoft.Extensions.Configuration.EnvironmentVariables.

这篇关于构建.NET的核心应用失败由于缺少定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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