.net核心控制台应用程序强类型配置 [英] .net core Console application strongly typed Configuration

查看:58
本文介绍了.net核心控制台应用程序强类型配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET Core控制台应用程序上,我试图将设置从自定义appsettings.json文件映射到自定义配置类.

我已经在线查看了一些资源,但是无法获得.Bind扩展方法的工作(我认为它可以在asp.net应用程序或.Net Core的早期版本上使用,如大多数示例所示)./p>

这是代码:

 静态void Main(string [] args){var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json",可选:false,reloadOnChange:true);IConfigurationRoot配置= builder.Build();//这是一个自定义配置对象配置设置= new Configuration();//将GetSection的结果绑定到Configuration对象//无法使用.Bind扩展名configuration.GetSection("MySection");//.Bind(settings);//我可以像这样手动映射MySection中的每个项目settings.APIBaseUrl = configuration.GetSection("MySection")["APIBaseUrl"];//我希望完成的工作是将该部分映射到我的Configuration对象//但是这给了我错误://IConfigurationSection不包含Bind的定义//控制台应用程序是否可以解决此问题//或者我是否必须手动映射每个项目?settings = configuration.GetSection("MySection").Bind(settings);//我可以在调用单个键时得到结果Console.WriteLine($"Key1 = {configuration [" MySection:Key1]}"));Console.WriteLine("Hello World!");} 

是否可以将GetSection("MySection")中的结果自动映射到自定义对象?这适用于在.NET Core 1.1上运行的控制台应用程序

谢谢!

解决方案

您需要添加NuGet包 Microsoft.Extensions.Configuration.Binder 使其在控制台应用程序中工作.

On an .NET Core Console app, I'm trying to map settings from a custom appsettings.json file into a custom configuration class.

I've looked at several resources online but was not able to get the .Bind extension method work (i think it works on asp.net apps or previous version of .Net Core as most of the examples show that).

Here is the code:

 static void Main(string[] args)
    {

        var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        IConfigurationRoot configuration = builder.Build();

        //this is a custom configuration object
        Configuration settings = new Configuration();

        //Bind the result of GetSection to the Configuration object
        //unable to use .Bind extension
        configuration.GetSection("MySection");//.Bind(settings);

        //I can map each item from MySection manually like this
        settings.APIBaseUrl = configuration.GetSection("MySection")["APIBaseUrl"];

        //what I wish to accomplish is to map the section to my Configuration object
        //But this gives me the error:
        //IConfigurationSection does not contain the definition for Bind
        //is there any work around for this for Console apps
        //or do i have to map each item manually?
        settings = configuration.GetSection("MySection").Bind(settings);

        //I'm able to get the result when calling individual keys
        Console.WriteLine($"Key1 = {configuration["MySection:Key1"]}");

        Console.WriteLine("Hello World!");
    }

Is there any approach to auto map the results from GetSection("MySection") to a custom object? This is for Console Application running on .NET Core 1.1

Thanks!

解决方案

You need to add the NuGet package Microsoft.Extensions.Configuration.Binder to get it to work in a console application.

这篇关于.net核心控制台应用程序强类型配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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