.NET 6 - IOptions issue

Viewed 294

In the past, in .NET 3.1 I wrote an extension method (C#) that we used to setup some basic pipeline stuff when developing an API. One of these things was registration of some configuration classes via IOptions, so we could inject them into other classes, managers, controllers...

Now, we switched to .NET 6 and for some reason, the same thing is not working any longer, and I can't get it figured out.

This is my extension method

public static IServiceCollection AddDefaultApiSetup(
    this IServiceCollection services, IConfiguration configuration)
{
    //Method 1 I tried, from Microsoft Documentation:
    // docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options
    services.Configure<HttpClientConfiguration>(
    configuration.GetSection(Constants.HttpClientConfigurationKey));

    //Method 2 I tried
    var httpClientsConfig =
        configuration.GetSection(Constants.HttpClientConfigurationKey)
            .Get<HttpClientConfiguration>();
    services.Configure<HttpClientConfiguration>(
        Constants.HttpClientConfigurationKey,configuration);

    return services.AddOtherStuff();
}

In the above method, using method 2, the httpClientsConfig variable does contain the correct values, but when injecting the IOptions in a class, the object is empty. Using method 1, it just doesn't work at all :)

If I would move the code directly into the Startup.cs class directly using the builder.Services and builder.Configuration, it also doesn't work (d'uuuh), so I must be doing something wrong, and I can't seem to find anything about this, as all sites seem to be talking about .NET Core 3.1 examples

Of course, I could just inject IConfiguration in my classes and then read the specific section there, but I just don't like that, as I want to only have the info I need in some controllers, not the whole config.

Any tips, suggestions, remarks...? PS: It also doesn't help changing IConfiguration to IConfigurationRoot or something

Update: appSettings file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "HttpClients": {
    "AppName": "My API",
    "AppVersion": "1.0",
    "Clients": [
      {
        "Name": "WebApiCore",
        "BaseUri": "https://localhost:4520"
      }
    ]
  }
}

Update: added the class where I'm reading the configuration into. Remind that 'reading the configuration into the class' works, but not via the IOptions method... Injecting IConfiguration in my class and then doing the below just works, but I want to use IOptions

var config = _configuration.GetSection(Constants.HttpClientConfigurationKey).Get<HttpClientConfiguration>();
    public class HttpClientConfiguration
    {
        public string AppName { get; set; } = string.Empty;
        public string AppVersion { get; set; } = string.Empty;
        public List<ApiConfiguration> Clients { get; set; } = new List<ApiConfiguration>();
    }
0 Answers
Related