Asp.net 5有一个新的配置系统,它利用json文件进行设置。您必须使用新Flight<Integer>
类的interface
方法手动选择要加载的json文件作为配置。
与此同时,在Azure领域,我们已经获得了这个漂亮的interface
类,如果找不到Azure设置,该类应该处理来自Azure网站设置或活动.AddJsonFile(string fileName)
的抓取设置。
但这两件事如何协同工作?我找不到任何文件有困难。我想在Azure中管理我的设置以进行生产,但是使用Configuration
进行本地调试。我在查找任何示例或文档方面遇到了很多麻烦。
答案 0 :(得分:22)
嗯,事实证明,当使用CloudConfigurationManager和asp.net 5时,答案是你不,并且样板代码已经涵盖了它。 (感谢Scott Hanselman在推特上回复我)
所以标准方法是这样的:
IConfiguration configuration = new Configuration()
.AddJsonFile("config.json") // adds settings from the config.json file
.AddEnvironmentVariables(); // adds settings from the Azure WebSite config
调用它们的顺序意味着环境变量中的设置将覆盖本地配置中的设置。要使用此功能,您需要确保Azure设置将模仿您的Json设置 - 所以如果您的json文件看起来像
{
"AppSettings": {
"ConnectionString": "blahblahblah"
}
}
您希望将您的设置设置为azure,使其看起来像
Key: AppSettings:ConnectionString
Value: blahblahblah
,然后您可以继续使用您用于本地配置的完全相同的代码。
var connectionString = Configuration.Get("AppSettings:ConnectionString");