在没有IOptions< T>的情况下从appsettings.json中读取和使用设置吗?

编程入门 行业动态 更新时间:2024-10-15 22:28:51
本文介绍了在没有IOptions< T>的情况下从appsettings.json中读取和使用设置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

.NET Core 允许从配置文件中懒惰地读取设置,将其反序列化为POCO,然后使用一行代码将POCO注册到内置DI容器中:

.NET Core allows to lazily read settings from configuration file, de-serialize it to a POCO and register that POCO in built-in DI container with one line of code:

public void ConfigureServices(IServiceCollection services) { services.Configure<MyOptions>(Configuration.GetSection("MySection")); }

然后,任何消费者都可以解析 IOptions< MyOptions> 访问该POCO:

Then any consumer can resolve IOptions<MyOptions> to access that POCO:

public HomeController(IOptions<MyOptions> optionsAccessor) { MyOptions options = optionsAccessor.Value; }

这种方法有很多缺点:

  • Microsoft.Extensions.Options 包中不必要的依赖项:

  • Unnecessary dependency from Microsoft.Extensions.Options package:

模拟,测试和显式实例创建变得更加冗长。

Mocking, testing and explicit instance creation become a bit more verbose.

什么是没有 IOptions< T> 的最简单解决方案?

What is the easiest solution to resolve options without IOptions<T>?

推荐答案

反序列化 configuration.Get< TOptions> 或 configuration.Bind 的选项,并在DI容器中将POCO明确注册为单例:

Deserialize options with configuration.Get<TOptions> or configuration.Bind call and register a POCO in DI container explicitly as singleton:

public void ConfigureServices(IServiceCollection services) { services.AddSingletonFromFile<MyOptions>(Configuration.GetSection("MySection")); } //... public static IServiceCollection AddSingletonFromFile<TOptions>( this IServiceCollection services, IConfiguration configuration) where TOptions : class, new() { //POCO is created with actual values TOptions options = configuration.Get<TOptions>(); services.AddSingleton(options); return services; }

UPD:感谢@NPNelson提供的。Get< TOptions>()提示。

UPD: thanks to @NPNelson for .Get<TOptions>() hint.

然后 IOptions< T> 解析不再

public HomeController(MyOptions options) { _options = options; }

仅供参考:也可以从外部服务(数据库等)读取方式:

FYI: reading from an external service (database etc.) is also possible this way:

public void ConfigureServices(IServiceCollection services) { services.AddTransientFromService<OptionsReader, MyOptions>(reader => reader.GetOptions()); } //... public static void AddTransientFromService<TReader, TOptions>( this IServiceCollection services, Func<TReader, TOptions> getOptions) where TReader : class where TOptions : class { services.AddTransient(provider => getOptions(provider.GetService<TReader>())); }

备注:

  • Singleton并不懒惰(总是在启动时实例化);
  • 如果是单例注册,则在运行时会丢失任何更新选项的功能(.NET Core通过 reloadOnChange 选项设置本地支持运行时文件重新加载: .AddJsonFile( appsettings.json,false,reloadOnChange:true));
  • Singleton is not lazy (it's always instantiated at startup);
  • In case of singleton registration, any ability to update options at runtime is lost (.NET Core natively supports runtime file reloading with reloadOnChange option setup: .AddJsonFile("appsettings.json", false, reloadOnChange: true));

如果您确实需要重新加载文件,但仍然不想使用 IOptions ,请考虑临时解析。当然,按请求解决可能会导致性能显着下降。

If you really need the file reload and you still don't want to use IOptions, consider a transient resolving. Of course, per-request resolving can lead to the significant perfomance decrease.

更多推荐

在没有IOptions&lt; T&gt;的情况下从appsettings.json中读取和使用设置吗?

本文发布于:2023-11-16 20:30:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   lt   IOptions   amp   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!