在mvc6中全局使用IConfiguration [英] Using IConfiguration globally in mvc6

查看:481
本文介绍了在mvc6中全局使用IConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能已经盯着这个很长时间,但我已经跳入MVC6为asp.net在过去几天,虽然我真的很喜欢这个,我似乎找不到一个方便的方式访问配置在Start.cs中用

定义后

  Configuration = new Configuration()
.AddJsonFile( json)
...

所以,我需要将它添加到DI或者它已经在那里?或者我应该创建一个新的实例每当我需要使用它,因为它可以创建不同的配置(如为IIdentityMessageService)创建一个sendgrid.json,并在服务本身加载?


$

解决方案

p>只能在Startup.cs中加载配置。如果您以后需要它们,您可以将值加载到适当的POCO中,并在DI中注册它们,以便您可以在需要的地方注入它们。这允许您以适合您的应用程序的方式在不同的文件中以及在不同的POCO中组织配置。在依赖注入中已经内置了对此的支持。













$ b

  public class SomeOptions 
{
public string EndpointUrl {get;组; }
}

您的Startup.cs将配置加载到POCO中, DI。

  public class Startup 
{
public Startup b $ b {
Configuration = new Configuration()
.AddJsonFile(Config.json)
.AddEnvironmentVariables();
}

public IConfiguration Configuration {get;组; }

public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}

public void ConfigureServices(IServiceCollection services)
{
services.Configure< SomeOptions>(options =>
options.EndpointUrl = Get(EndpointUrl));

services.AddMvc();
}
}

然后在您的控制器中获取您创建的配置POCO Startup.cs通过依赖注入如下:

  public class SomeController 
{
private string _endpointUrl;
public SomeController(IOptions< SomeOptions> options)
{
_endpointUrl = options.Options.EndpointUrl;
}
}

使用1.0.0-beta1构建的aspnet5测试。



有关详情,请参阅 ASP.Net 5配置的基本原理


I have Probably been staring at this for to long, but I have jumped into MVC6 for asp.net for the last few days, while I am really enjoying this, I can't seem to find a convenient way to access the Configuration after it is defined in Start.cs with

Configuration = new Configuration()
    .AddJsonFile("config.json")
    ...

So, do I need to add it to the DI or is it already there? Or should I create a new instance whenever I need to use it, as it is possible to create different configurations (such as for IIdentityMessageService) create a sendgrid.json and load it within the Service itself?

There is probably a very easy solution, but like I said I have looking at this for days now.

解决方案

Only do the loading of Configurations in your Startup.cs. If you need them elsewhere later you could load the values into appropriate POCOs and register them in the DI so you can inject them where you need them. This allows you to organize your configuration in different files, and in different POCOs in a way that makes sense to your application. There is already built in support for this in the dependency injection. Here is how you would do it:

A POCO to put your configuration in:

public class SomeOptions
{
    public string EndpointUrl { get; set; }
}

Your Startup.cs loads the configuration into the POCO and registers it in the DI.

public class Startup
{
    public Startup()
    {
        Configuration = new Configuration()
                    .AddJsonFile("Config.json")
                    .AddEnvironmentVariables();
    }

    public IConfiguration Configuration { get; set; }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<SomeOptions>(options => 
            options.EndpointUrl = Configuration.Get("EndpointUrl"));

        services.AddMvc();
    }
}

Then in your controller get the configuration POCO you created in the Startup.cs through dependency injection like this:

public class SomeController
{
    private string _endpointUrl;
    public SomeController(IOptions<SomeOptions> options)
    {
        _endpointUrl = options.Options.EndpointUrl;
    }
}

Tested with 1.0.0-beta1 builds of aspnet5.

For more information see The fundamentals of ASP.Net 5 Configuration.

这篇关于在mvc6中全局使用IConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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