2016-08-01 72 views
0

我想设置一个ASP.Net核心应用程序从json文件中读取配置设置。我正在使用VS2015和.NetCore 1.0(使用.Net Core Tools预览版2)。我在获取一段简单的锅炉板代码进行编译时遇到了问题。Startup.cs错误(ASP.Net核心配置)

我使用下面的代码,这是在 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接受型IConfigurationBuilder的第一个参数的定义,并没有扩展方法AddApplicationInsightsSettings可以发现

任何建议吗? 感谢

回答

7

只需Configuration属性添加到您的Startup类,教程已经错过了“一步”:

public IConfigurationRoot Configuration { get; set; } 

ConfigurationBuilder.Build()方法只是返回的IConfigurationRoot情况下,你应该保存,如果需要进一步获取设置启动类(例如ConfigureServices方法)。

关于第二个错误,貌似你没有添加Application Insights依赖性:

{ 
    "dependencies": { 
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0" 
    } 
} 
+0

非常感谢这个问题的答案。有关第二个错误的任何线索?谢谢。 – Hughgo

+0

已更新回答 – Set

+0

谢谢 - 我错过了。 – Hughgo