Startup.cs错误(ASP.Net核心配置)

时间:2016-08-01 08:10:53

标签: configuration visual-studio-2015 asp.net-core

我正在尝试设置ASP.Net Core应用程序以从json文件中读取配置设置。我正在使用VS2015和.NetCore 1.0(带.Net Core Tools预览版2)。我在编写简单的锅炉板代码时遇到了问题。

我正在使用以下代码,该代码已发布于 http://asp.net-hacker.rocks/2016/03/21/configure-aspnetcore.html

 public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();

        if (env.IsDevelopment())
        {
            // This will push telemetry data through Application Insights 
            // pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }
        Configuration = builder.Build();
    }

然而,IDE /编译器抱怨“名称”配置“在当前上下文中不存在”(最后一行代码)。 IDE的唯一建议是包含Microsoft.Extensions.Configuration。但是,这是一个名称空间,它不包含名为“Configuration”的对象或属性。

此外,“AddApplicationInsightsSettings”失败,IConfigurationBuilder不包含AddApplicationInsightsSettings的定义,也没有扩展方法AddApplicationInsightsSettings接受类型IConfigurationBuilder的第一个参数可以找到

有什么建议吗? 感谢

1 个答案:

答案 0 :(得分:7)

只需将Configuration属性添加到Startup课程,教程就错过了这个'步骤':

public IConfigurationRoot Configuration { get; set; }

ConfigurationBuilder.Build()方法只返回IConfigurationRoot的实例,如果需要在Startup类中进一步设置(例如,在ConfigureServices方法中),则应保存该实例。

关于第二个错误,看起来您没有添加Application Insights依赖项:

{
  "dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0"
  }
}
相关问题