0

I have some settings in my appSettings.json file that i would like to retrieve. In order to do get values from the appSettings file I did the below

private IConfiguration configuration;

public class MyController
{
     public MyController(IConfiguration configure)
     {
          configuration = configure;
     }
}

In my method i then did something like

var someString = configuration.GetValue<string>("Smtp:Host");

This returned the value of the SMTP host form the config file which is what i wanted.

I then came across another Controller that also required this same setting so instead of copying and pasting from one controller to another i decided to create a new static class in a class library.

I declared the class

public static class GlobalConfiguration
{
 private static readonly IConfiguration config;

 public static string Host {get;} = config.GetValue("Smtp:host");
}

Tried to use it and it threw an error (object not set to an instance).

How could i bring IConfiguration into a static class so i can use this same setting across my app rather than having to paste it from one Controller to another?

Computer
  • 2,149
  • 7
  • 34
  • 71

2 Answers2

2

How could I bring IConfiguration into a static class so i can use this same setting across my app

You don't. By going the static configuration class route, you go against DI.

Create a class SmtpSettings to hold the settings:

public class SmtpSettings
{
    [Required]
    public string? Host { get; set; }
}

Register IOptions<SmtpSettings>:

.ConfigureServices((context, services) =>
{
    services.Configure<SmtpSettings>(context.Configuration.GetSection("Smtp"));
});

And inject that into controllers that need those settings:

public class MyController
{
    private readonly SmtpSettings _smtpSettings;

    public MyController(IOptions<SmtpSettings> smtpSettings)
    {
        _smtpSettings = smtpSettings.Value;
    }
}

Read more about the Options pattern here.

You could instead assign the static class's config during application startup though, after building the host:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

GlobalConfiguration.Config = app.Configuration;

await app.RunAsync();
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I didnt use the static config you mentioned towards the end of your answer as the first way seems to have worked nicely. I dont know how you figured out how to do that but if you do have a book/course i could read up on then please do mention. Thanks again – Computer May 05 '23 at 14:49
0

Another option here would be to create a new IMySettings interface and register that for DI as a singleton. The instance of MySettings could be set with the values from IConfiguration during the IoC registration once and then anything that needs it can just inject IMySettings, for example.

Or you could use the IOptions<> and do that, but I personally like having my own settings class that I inject.

Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39