我们如何将 CloudConfigurationManager 与 asp.net 5 JSON 配置一起使用? [英] How do we use CloudConfigurationManager with asp.net 5 JSON configs?

查看:14
本文介绍了我们如何将 CloudConfigurationManager 与 asp.net 5 JSON 配置一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 网站设置或活动的 web.config 获取设置,如果未找到 Azure 设置.

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"
  }
}

您希望在 azure 中设置您的设置,使其看起来像

You'd want to set up your setting in azure to look like

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆