"IConfiguration"没有包含"Get"的定义。

62

最初我使用的代码版本是1.0.0-rc1-beta6。

 public static IConfiguration Configuration { get; set; }

 public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        // Setup configuration sources.
        var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();
        var test = Configuration.Get("ASPNET_ENV");
    }

现在我想使用1.0.0-rc1-update版本的代码:

 public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        // Setup configuration sources.
        var builder = new ConfigurationBuilder()
            .SetBasePath(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();
        var test = Configuration.Get("ASPNET_ENV");
    }

"ASP.NET_ENV"是来自launchSettings.json文件的。

"profiles": {
"IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "environmentVariables": {
    "ASPNET_ENV": "Development"
  },
  "sdkVersion": "dnx-clr-win-x64.1.0.0-beta6"
},

但我仍然在最后一行收到错误。

'IConfiguration'不包含“Get”的定义,而最佳扩展方法重载'SessionExtensions.Get(ISession, string)'需要一个类型为“ISession”的接收器。

这里也收到了错误。

   public void ConfigureServices(IServiceCollection services)
    {
        // Add MVC services to the services container.
        services.AddMvc(); //error
    }

我的 project.json 文件

    {
  "webroot": "wwwroot",
  "version": "1.0.0-*",
  "dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta8",
    "Microsoft.Framework.Logging": "1.0.0-beta8",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
    "EntityFramework": "6.1.3",
    "jqGridWebApi": "1.1.4",
    "EnyimMemcached": "2.13.0",
    "xunit": "2.2.0-beta1-build3239",
    "Moq": "4.2.1510.2205",
    "NLog": "2.1.0",
    "CryptSharpOfficial": "2.1.0",
    "System.Linq.Dynamic": "1.0.4"
  },
  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001",
    "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5005"
  },
  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.configuration": "4.0.0.0",
        "System.Data": "4.0.0.0"
      }
    }
  },
  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  },
  "configurations": {
    "Staging": { }
  }
}

尝试使用 GetValue() 或 GetValue<T>()。 - Soren
请将 IconfigurationSection 绑定到复杂对象,而不使用 AspNetCore。 - JohnB
3个回答

182

4
这应该是被接受的答案,因为它直接回答了问题提出者的问题。 - Chad Lehman
2
这是“THE”解决方案,谢谢。 - Eman
3
请注意,您不需要为Binder添加using子句,只需添加“using Microsoft.Extensions.Configuration;”即可。 - kristianp
这对我来说还不够,配置对象是使用Configuration而不是IConfiguration进行构造函数注入的。因此错误是正确的,但原因是一个微妙的拼写错误。 - Aaron
哦,谢谢!一直在尝试所有的属性,但现在我明白了。 - steb

8

现在已经不存在Get方法了。你需要使用:

Configuration.GetSection("key").Value.ToString();

1
对于RC1版本,配置的NuGet包在Microsoft.Extensions.Configuration.Json中,而不是Microsoft.Framework.Configuration.Json
public Startup(IHostingEnvironment env,  IApplicationEnvironment appEnv)
{
    // Setup configuration sources.
    var builder = new ConfigurationBuilder()
        .SetBasePath(appEnv.ApplicationBasePath)
        .AddJsonFile("config.json", false)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
    var test = Configuration.Get<string>("ASPNET_ENV");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接