unoplatform / uno.extensions

Libraries to ease common developer tasks associated with building multi-platform mobile, desktop and web applications using Uno Platform or WinAppSDK.
https://platform.uno/
Other
72 stars 45 forks source link

IConfigBuilder.Section doesn't work with configurationSection param, works with configSection #2330

Open ttustonic opened 3 months ago

ttustonic commented 3 months ago

Current behavior

Setting configurationSection parameter doesn't work, configuration is not read from config file when the config section doesn't match the class name Setting configSection works

Expected behavior

Both configurationSectionand configSectionshould work the same.

How to reproduce it (as minimally and precisely as possible)

Attached is a new project from the wizard. In the project there's a new config class, AppTest. public record AppTest { public string? InfoTitle { get; init; } }

In the config file there's a section "AppTestTEST": { "InfoTitle": "AppInfo title" } in UseConfiguration there's a line: .Section<AppTest>(configurationSection: "AppTestTEST") which doesn't work (value of InfoTitle is null), but .Section<AppTest>(configSection: (hbc) => hbc.Configuration.GetSection("AppTestTEST")) works as expected.

MainViewModelhas an additional parameter, IOptions<AppTest> appTest, and new property TestTitle which is bound to the Textbox in the MainPage.xaml

Environment

Nuget Package (s): default packages from the Uno Platform App wizard Package Version(s):

Affected platform(s):

Visual Studio:

Relevant plugins:

Anything else we need to know?

UnoApp1.zip

agneszitte commented 3 months ago

cc @nickrandolph, @dr1rrb, @eriklimakc

kazo0 commented 1 month ago

Hi @ttustonic!

It seems that IOptions does not support Named options.

In order to access the options for a specific name you would need to inject an IOptionsSnapshot<AppTest> myConfig instead of an IOptions in the constructor and retrieve the named section by calling myConfig.Get("AppTestTEST")

Let me know if that works for you!