Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use IOptions in WPF CORE 5?

I have a worker service app that uses different appsettings files per deployment i.e. Dev, Test, Prod. The template starts with the Program static void main as usual and then goes on to IHostBuilder as usual...

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
    var host = Host.CreateDefaultBuilder(args)
    .UseWindowsService()
    .ConfigureServices((hostContext, services) =>
    {
        IConfiguration config = hostContext.Configuration;
        // more stuff using config including...
        services.Configure<KepwareSettings>(config.GetSection("KepwareSettings"));
    }
 }

This is working just fine. Note that IOptions is supported with services.Configure<class>(config.GetSection("mysection")) I'm also using appsettings per environment in a WPF app that uses code from Tim Corey:

public App()
{
    ServiceCollection services = new ServiceCollection();
    ConfigureServices(services);
    _serviceProvider = services.BuildServiceProvider();
}

private IConfiguration GetConfiguration()
{
    IConfigurationBuilder builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

    builder.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")}.json");

    return builder.Build();
}

private void ConfigureServices(ServiceCollection services)
{
    IConfiguration config = GetConfiguration();
    string conn = config.GetConnectionString("Neenah_SFC_ConnectionString");

    services.AddSingleton(config);

    // more stuff with config but with ERROR on this line
    services.Configure<TestSettings>(config.GetSection("TestSettings"));
}

The services.Configure isn't working. The error I get is cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<MyApp.TestSettings> I've been comparing all the Nuget packages and all the usings at the top. I made sure that Microsoft.Extensions.DependencyInjection and .Configuration are the same in Nuget. When I look at the declarations from metadata for IServicesCollection.Configure they're different.

public static IServiceCollection Configure<[DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes)(-1))] TOptions>(this IServiceCollection services, IConfiguration config) where TOptions : class;

public static IServiceCollection Configure<TOptions>(this IServiceCollection services, Action<TOptions> configureOptions) where TOptions : class;

The first one is from the worker service app that works. The second one comes from the WPF app that doesn't work. The metadata for the first one says Microsoft.Extensions.Options.ConfigurationExtensions which is not in any Nuget packages I have set up..

Worker App Nuget Packages

I tried adding the Nuget package Microsoft.Extensions.Options and added the using statement, no luck there. I've spent hours searching and found examples of console apps, web app .NET API, per deployment settings in MVC, and WPF. But I can't find the IOptions pattern with services.Configure<MySettings>(config.GetSection("MySettings")) in a WPF app that I get from NET CORE 5 WPF template. What's the right way to do this?

Thanks, Mike

like image 975
Hey Mikey Avatar asked Nov 01 '25 23:11

Hey Mikey


1 Answers

try adding this package: Microsoft.Extensions.Options.ConfigurationExtensions

like image 170
blindmeis Avatar answered Nov 03 '25 12:11

blindmeis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!