1

I am trying to access my settings in order to configure my services. I am using AWS systems manager to store my settings

// Settings class
public class Settings
{
    public string Url { get; set; }
}

// my Program.cs code
var builder = WebApplication.CreateBuilder();

// Clear default sources
builder.Configuration.Sources.Clear();

// Get configuration
builder.Configuration.AddSystemsManager(source =>
{
     source.Path = $"/{env}";
     source.ParameterProcessor = new DefaultParameterProcessor();
});
        
// Load up the settings
builder.Services.Configure<Settings>(builder.Configuration.GetSection(appName));

// Access the settings to use for configuring the services
var settings = builder.Configuration.Get<Settings>();

When I check the settings object, the values are null. However when I inject the settings into an endpoint using IOptions<Setting>, the values are populated.

I tried the .Get<Settings> after I called Build();, but that also returned null values. Is there something I am missing?

2 Answers 2

2

You said you want to access the settings to configure some services. You can accomplish that with something like:

builder.Services.AddSingleton<IMyService>(x => {
    // x is IServiceProvider
    var settings = x.GetRequiredService<IOptions<Settings>>();
    var url = settings.Value.Url;
    if(url is null)
        throw new Exception("missing url");
    var myservice = new MyService(url);
    // this is the instance that will be used, use singleton/scoped/transient as needed
    return myservice;
});

where MyService is the service you want to configure, if not needed you can avoid the IMyService interface and of course you need to use the desired lifetime (singleton/scoped/transient)

Doing so though will somehow force you to explicitly declare the dependencies of "MyService".

Another way of fetching that value is:

// get the configuration section
var section = builder.Configuration.GetSection("appname:Settings");
// create the POCO
var settings = new Settings();
// bind the values to the POCO
section.Bind(settings);
// you can access the value
var yourUrl = settings.Url;
// you can still inject it as IOption<>
builder.Services.Configure<Settings>(section);

I am assuming you have an "appname" section with a "Settings" section in your configuration, if not then use .GetSection("appname"); instead of .GetSection("appname:Settings");

Sign up to request clarification or add additional context in comments.

Comments

0

According to the [docs], you should be able to retrieve your options using extension method: builder.Configuration.GetAWSOptions()

As an alternative, if you have those Settings options in appsettings.json you should be able to retrieve those using: builder.Configuration["Settings:Url"]

Also, as you have used builder.Configuration.GetSection(appName) doesn't it work for you when you try to retrieve options using: builder.Configuration.GetSection("Settings")?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.