如何在 ASP.NET Core 的任何类中访问 Configuration? [英] How do I access Configuration in any class in ASP.NET Core?

查看:25
本文介绍了如何在 ASP.NET Core 的任何类中访问 Configuration?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于 ASP.NET 核心的配置文档.文档说您可以从应用程序的任何位置访问配置.

I have gone through configuration documentation on ASP.NET core. Documentation says you can access configuration from anywhere in the application.

下面是模板创建的Startup.cs

Below is Startup.cs created by template

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsEnvironment("Development"))
        {
            // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseApplicationInsightsRequestTelemetry();

        app.UseApplicationInsightsExceptionTelemetry();

        app.UseMvc();
    }
}

所以在Startup.cs中我们配置了所有的设置,Startup.cs还有一个名为Configuration

So in Startup.cs we configure all the settings, Startup.cs also has a property named Configuration

我无法理解您如何在控制器或应用程序的任何地方访问此配置?MS 建议使用 options pattern 但我只有 4-5 个键值对,所以我不想使用选项模式.我只是想访问应用程序中的配置.我如何在任何类中注入它?

What I'm not able to understand how do you access this configuration in controller or anywhere in the application? MS is recommending to use options pattern but I have only 4-5 key-value pairs so I would like not to use options pattern. I just wanted to have access to Configuration in application. How do I inject it in any class?

推荐答案

更新

使用 ASP.NET Core 2.0 将 自动在依赖注入容器中添加应用程序的IConfiguration实例.这也可以与 WebHostBuilder 上的 ConfigureAppConfiguration 结合使用.

Update

Using ASP.NET Core 2.0 will automatically add the IConfiguration instance of your application in the dependency injection container. This also works in conjunction with ConfigureAppConfiguration on the WebHostBuilder.

例如:

public static void Main(string[] args)
{
    var host = WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(builder =>
        {
            builder.AddIniFile("foo.ini");
        })
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

<小时>

就像将 IConfiguration 实例作为 ConfigureServices 中的单例对象添加到服务集合中一样简单:


It's just as easy as adding the IConfiguration instance to the service collection as a singleton object in ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
   services.AddSingleton<IConfiguration>(Configuration);

   // ...
}

其中 Configuration 是您的 Startup 类中的实例.

Where Configuration is the instance in your Startup class.

这允许您在任何控制器或服务中注入 IConfiguration:

This allows you to inject IConfiguration in any controller or service:

public class HomeController
{
   public HomeController(IConfiguration configuration)
   {
      // Use IConfiguration instance
   }
}

这篇关于如何在 ASP.NET Core 的任何类中访问 Configuration?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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