我们如何将CloudConfigurationManager与asp.net 5 JSON配置一起使用? [英] How do we use CloudConfigurationManager with asp.net 5 JSON configs?
问题描述
Asp.net 5具有一个新的配置系统,该系统利用json文件进行设置.您必须使用新的Configuration
类的.AddJsonFile(string fileName)
方法手动选择要作为配置加载的json文件.
Asp.net 5 has a new configuration system which utilizes json files for settings. You have to manually select the json file you want loaded as your configuration with the .AddJsonFile(string fileName)
method of the new Configuration
class.
同时,在Azure领域中,我们有一个漂亮的CloudConfigurationManager
类,该类应该处理从Azure网站设置中捕获的设置,或者在未找到Azure设置的情况下从活动的web.config
中处理抓取设置.
Meanwhile, in Azure land we've got this nifty CloudConfigurationManager
class which is supposed to handle grabbing settings from the Azure Website settings or the active web.config
if a Azure setting is not found.
但是,这两件事是如何协同工作的?我在查找任何文档时遇到麻烦.我想在Azure中管理我的设置以进行生产,但使用config.json
进行本地调试.在查找任何示例或文档时,我遇到了很多麻烦.
But how are these two things intended to work together? I'm having trouble finding any documentation. I'd like to manage my settings in Azure for production but use the config.json
for local debugging. I've had a great deal of trouble finding any examples or documentation.
推荐答案
好吧,事实证明,将CloudConfigurationManager与asp.net 5结合使用时,答案是您不 ,样板代码已经涵盖了它. (感谢Scott Hanselman在Twitter上回复我)
Well, it turns out that when it comes to using CloudConfigurationManager with asp.net 5, the answer is that you don't, and the boilerplate code already covered it. (Thanks to Scott Hanselman for getting back to me on twitter)
所以标准方法是这样的:
So the standard approach is something like this:
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文件看起来像
The order in which these are called means that settings from the environment variables will over-write settings from the local config. All you have to do to make use of this is make sure the Azure settings will mimic your Json settings- so if your json file looks like
{
"AppSettings": {
"ConnectionString": "blahblahblah"
}
}
您希望将天蓝色设置为
Key: AppSettings:ConnectionString
Value: blahblahblah
,然后可以继续使用与本地配置完全相同的代码.
, and then you can go ahead and use the exact same code you'd use for the local config.
var connectionString = Configuration.Get("AppSettings:ConnectionString");
这篇关于我们如何将CloudConfigurationManager与asp.net 5 JSON配置一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!