【问题标题】:Best practices for config file in netcoreapp1.1netcoreapp1.1 中配置文件的最佳实践
【发布时间】:2017-09-06 01:18:59
【问题描述】:

我已将 appsettings.json 文件的访问权限添加为我的 Startup.cs 中的框架服务:

public IConfigurationRoot Configuration { get; }

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

// 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.Configure<AppConfig>(Configuration);
    services.AddMvc();
}

所以现在我可以从我的控制器访问配置文件了:

   public class HomeController : Controller
    {
        private readonly AppConfig _appConfig;

        public HomeController(IOptions<AppConfig> appConfig, ConfigContext configContext)
        {
            _appConfig = appConfig.Value;
        }
}

这行得通,但目前在 netcoreapps 中从我的控制器范围之外的类访问配置文件的良好做法是什么?

我的意思是我不想总是将所需的配置变量传递给其他方法,例如:

public IActionResult AnyAction() {
  SomeStaticClass.SomeMethod(_appConfig.var1, _appConfig.var2, _appConfig.var3...)

 //or always have to pass the _appConfig reference

  SomeStaticClass.SomeMethod(_appConfig)
}

在以前版本的 .NET Framework 中,如果我需要从“SomeStaticClass”访问配置文件,我曾经在需要访问 web.config 的任何类中使用 ConfigurationManager。

在 netcoreapp1.1 中正确的做法是什么? ConfigurationManager 之类的方法或依赖注入方法都适合我。

【问题讨论】:

标签: asp.net-core asp.net-core-mvc asp.net-core-1.1


【解决方案1】:

我所做的是创建以下类:

public static class Configuration
{
    private static IConfiguration _configuration;

    static Configuration()
    {
        var builder = new ConfigurationBuilder()
         .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

        _configuration = builder.Build();
    }

    public static string GetValue(string key)
    {
        return _configuration.GetValue<string>(key, null);
    }

    public static string GetValue(string section, string key)
    {
        return _configuration.GetSection(section).GetValue<string>(key);
    }
}

但是它不使用 Startup.cs 中使用 IHostingEnvironment 参数的环境逻辑。

【讨论】:

  • 请阅读依赖注入的工作原理。你可以在任何地方注入IOptions&lt;T&gt;(几乎)
【解决方案2】:

我认为这个问题更多的是关于如何从静态类中获取上下文变量。我认为这将完成你想要的,但我不确定你为什么想要一个静态类或你想用它做什么(参见 XY 问题)。

   public class HomeController : Controller
    {
        private readonly AppConfig _appConfig;

        public HomeController(IOptions<AppConfig> appConfig, ConfigContext configContext)
        {
            _appConfig = appConfig.Value;
            SomeStaticClass.SomeStaticMember = appConfig.Value
        }
        public IActionResult AnyAction() {
            SomeStaticClass.SomeMethod(); //The get the value you want from within
        }
    }

编辑: 您可以使用 Newtonsoft.Json,它是 Microsoft.AspNet.Mvc.ModelBinding 的依赖项,它是 Microsoft.AspNet.Mvc 的依赖项

string fileContents = string.Empty;
using (StreamReader file = File.OpenText(@".\appsettings.json"))
{
    fileContents = file.ReadAllLines();
}
configs= JsonConvert.DeserializeObject(fileContents );

【讨论】:

  • 不是真的,想法是从控制器外部的静态类访问 appsettings.json
  • 好的,我更新了我的答案。您可以使用 .\ 读取启动目录的根目录
猜你喜欢
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
  • 2012-03-09
  • 1970-01-01
  • 2023-03-06
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多