net6控制台应用,自定义配置程序问题

发布于 2025-02-13 11:41:32 字数 1160 浏览 0 评论 0原文

我有一个.NET6控制台应用程序,我需要从AppSettings.json检索一系列对象。目前,我正在使用“选项”类来检索数组。

class ConfigOpts { class subOpts { /* property accessors */ }, public List<subOpts> SubOpts {get;set;}}

var builder = new ConfigurationBuilder().SetBasePath(...).AddJsonFile(..);
IConfiguration config = build.build();
ConfigOpts opts = new ConfigOpts();
config.GetSection("ConfigOpts").Bind(opts);

给我一个包含子列表的对象。

需要在负载/节省时间进行解密和加密子OPT字段之一。因此,我想使用customConfigProvider并在Load()方法中进行解密,然后在createsave()方法中加密。

我已经修改了我的代码来添加新的类 configoptsprovider:configurationProvider,iconfigurationsource ,并创建了一个扩展方法addconfigoptsprovider,该方法在上面的粘合剂上称为。

这有效,我可以点击配置提供商中的Load()函数。

我的两个问题是: -

在加载方法中,如何读取appsettings.json文件(我查找了文档,但找到了用于读取数据库和其他文件的示例,但没有用于appsettings.json)。

另外,稍后如何调用CreateSave()函数?

问候。

更新 我注意到,因为我拥有configopts类,该类使我能够以类型的方式访问设置,因此尝试使用它可能很有用。

在负载中,我尝试将以下内容添加到LOAD()方法中。

((IConfiguration)this).GetSection("ConfigOpts").Bind(_configOptions);

该代码是我最初使用的(在上面的第一个示例中)。不幸的是,一个不能施放给偶像化。

如果这就是我需要做的,那就太好了。

I have a .Net6 Console application where I need to retrieve an array of objects from AppSettings.json. Currently I am using an 'options' class to retrieve the array.

class ConfigOpts { class subOpts { /* property accessors */ }, public List<subOpts> SubOpts {get;set;}}

and

var builder = new ConfigurationBuilder().SetBasePath(...).AddJsonFile(..);
IConfiguration config = build.build();
ConfigOpts opts = new ConfigOpts();
config.GetSection("ConfigOpts").Bind(opts);

Gives me an opts objects containing a list of subOpts.

One of the sub opt fields needs to be decrypted and encrypted at load/save time. So I would like to use a CustomConfigProvider and do the decrypt in the Load() method, and encrypt in a CreateSave() method.

I have amended my code to add a new class ConfigOptsProvider:ConfigurationProvider,IConfigurationSource and created an extension method AddConfigOptsProvider which is called on the binder above.

This works and I can hit the load() function in the config provider.

My two questions are:-

In the Load Method, how do I read the AppSettings.json file (I've looked for documentation but found examples for reading databases and other files but none for AppSettings.json).

Also, how do I call the CreateSave() function later?

Regards.

Update
I noticed as I have the ConfigOpts class that allows me to access the settings in a typed manner, it might be useful to try and use it.

In the load I have tried adding the following to the Load() method.

((IConfiguration)this).GetSection("ConfigOpts").Bind(_configOptions);

This code is what I originally used (in the first example above). Unfortunately one cant cast to IConfiguration.

It would be great if this is all I need to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2025-02-20 11:41:32

我的解决方案是使用system.text.json.nodes名称空间将AppSetting文件读取为JSON和它们处理。

我最终在Load()方法中使用了此例程,该方法填充了带有所需数据的内部自定义数据类,然后可以使用GET方法访问。由于数据类中的字段数量,这意味着我无法使用提供的数据属性返回结果。

var fp = _fileProvider.GetFileInfo("appsettings.json")
using (Stream s = fp.CreateReadStream())
{
    var co = JsonDocument.Parse(s)?.Deserialize<Dictionary<String,Object>>?.Where(str => str.Key == "ConfigOpt").FirstOrDefault();

    var subObts = JSonNode.Parse(co.GetValueOrDefault().Value.ToString())["SUbOpts"];
    foreach(JsonNOde opt in subOpts.AsArray())
    {
         var optName = opt["name"]?.AsValue; 
    }

}

我已经删除了大多数NULL检查和文件系统观察器,而且还没有对不同环境(AppSettings Files)的支票。结构是configopt:subopts:opt:subopts是opts数组的属性。

My solution was to use the System.Text.Json.Nodes namespace to read the appsettings files as JSON and them process.

I ended up with this routine in my Load() method which populated an internal custom data class with the required data, I can then access using a Get method. Because of the number of fields in the data Class, it meant I cannot use the provided Data property to return the result.

var fp = _fileProvider.GetFileInfo("appsettings.json")
using (Stream s = fp.CreateReadStream())
{
    var co = JsonDocument.Parse(s)?.Deserialize<Dictionary<String,Object>>?.Where(str => str.Key == "ConfigOpt").FirstOrDefault();

    var subObts = JSonNode.Parse(co.GetValueOrDefault().Value.ToString())["SUbOpts"];
    foreach(JsonNOde opt in subOpts.AsArray())
    {
         var optName = opt["name"]?.AsValue; 
    }

}

I have removed most of the null checks and the filesystem watcher and also there isn't a check for different environments (appsettings files). The structure is ConfigOpt:SubOpts:opt:property where SubOpts is an array of opts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文