ASP.NET 5(vNext) - 配置 [英] ASP.NET 5 (vNext) - Configuration

查看:101
本文介绍了ASP.NET 5(vNext) - 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习ASP.NET 5.我使用它在Mac OS X在这个时候,我有一个config.json文件如下所示:

I am trying to learn ASP.NET 5. I am using it on Mac OS X. At this time, I have a config.json file that looks like the following:

config.json

{
    "AppSettings": {
        "Environment":"dev"
    },

    "DbSettings": {
        "AppConnectionString": "..."
    },

    "EmailSettings": {
        "EmailApiKey": "..."        
    },  
}

我试图弄清楚如何将这些设置加载到Startup.cs的配置文件。目前,我有一个看起来像这样的文件:

I am trying to figure out how to load these settings into a configuration file in Startup.cs. Currently, I have a file that looks like this:

Configuration.cs

public class AppConfiguration
{
    public AppSettings AppSettings { get; set; }

    public DbSettings DbSettings { get; set; }

    public EmailSettings EmailSettings { get; set; }        
}

public class AppSettings
{
    public string Environment { get; set; }
}

public class DbSettings
{
    public string AppConnectionString { get; set; }
}

public class EmailSettings
{
    public string MandrillApiKey { get; set; }
}

然后,在Startup.cs,我有以下几点:

Then, in Startup.cs, I have the following:

public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
  var configuration = new Configuration().AddJsonFile("config.json");
  Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
  services.Configure<AppConfiguration>(Configuration);
} 

然而,该方法不工作。基本上,它像它不知道如何以.json和配置类之间进行映射。我该怎么做呢?

However, this approach doesn't work. Basically, its like it doesn't know how to map between the .json and the Config classes. How do I do this?

我真的想留在DI的方法,使我可以更有效地测试我的应用程序。

I would really like to stay with the DI approach so I can test my app more effectively.

感谢您

推荐答案

在你的启动类,先指定配置源:

In your Startup class, first specify your configuration sources:

private readonly IConfiguration configuration;

public Startup(IHostingEnvironment env)
{
    configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
}

env.EnvironmentName 是像发展生产的。假设它是发展。该框架将尝试从 config.development.json 拉配置值第一。如果请求的值不存在,或 config.development.json 文件不存在,那么该框架将尝试拉动值从 config.json

The env.EnvironmentName is something like "development" or "production". Suppose it is "development". The framework will try to pull configuration values from config.development.json first. If the requested values aren't there, or the config.development.json file doesn't exist, then the framework will try to pull the values from config.json.

配置您的 config.json 部分为这样的服务:

Configure your config.json sections as services like this:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(configuration.GetSubKey("AppSettings"));
    services.Configure<DbSettings>(configuration.GetSubKey("DbSettings"));
    services.Configure<EmailSettings>(configuration.GetSubKey("EmailSettings"));
}

电话配置及LT; T&GT;(IConfiguration) IServiceCollection 注册类型 IOptions&LT; T&gt;在依赖注入容器。请注意,您不需要你的 AppConfiguration 类。

Calling Configure<T>(IConfiguration) on the IServiceCollection registers the type IOptions<T> in the dependency injection container. Note that you don't need your AppConfiguration class.

然后指定被注入这样的:

Then specify dependencies to be injected like this:

public class EmailController : Controller
{
    private readonly IOptions<EmailSettings> emailSettings;

    public EmailController (IOptions<EmailSettings> emailSettings)
    {
        this.emailSettings = emailSettings;
    }

    public IActionResult Index()
    {
        string apiKey = emailSettings.Options.EmailApiKey;
        ...
    }
}

这篇关于ASP.NET 5(vNext) - 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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