Net Core:从Autofac模块访问appsettings.json值

编程入门 行业动态 更新时间:2024-10-25 08:17:49
本文介绍了Net Core:从Autofac模块访问appsettings.json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

AspNet核心应用程序

AspNet core app

1)像这样的Autofac模块

1) Autofac module like that

public class AutofacModule : Module { protected override void Load(ContainerBuilder builder) { //Register my components. Need to access to appsettings.json values from here } }

2 )步骤№1中的模块已注册到 Startup.cs

2) Module from step№1 registered into Startup.cs

public void ConfigureContainer(ContainerBuilder builder) { builder.RegisterModule(new AutofacModule()); }

如何访问 appsettings.json 值?我需要在 AutofacModule 中创建对象并将其用于DI。

How to access to appsettings.json values from AutofacModule? I need that for create my objects inside AutofacModule and using it for DI.

推荐答案

需要更改步骤№2

public void ConfigureContainer(ContainerBuilder builder) { //get settigns as object from config var someSettings= Configuration.GetSection(typeof(SomeSettings).Name).Get<SomeSettings>(); //put settings into module constructor builder.RegisterModule(new AutofacModule(someSettings)); }

我不知道是否是最佳实践方式,但是有效。

I don't know is "best practise" way or not, but it works.

更多推荐

Net Core:从Autofac模块访问appsettings.json值

本文发布于:2023-11-02 15:04:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   Core   Net   Autofac   json

发布评论

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

>www.elefans.com

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