Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AbsolutelyN 49 posts 331 karma points
    Oct 22, 2021 @ 12:40
    AbsolutelyN
    0

    How to inject a custom configuration section?

    Hi

    Could anyone clarify how to create and inject your own configuration into a custom service via dependency injection please?

    I'm registering a service and I need some configuration for an api key. I'm registering the service in a composer.

     builder.Services.AddSingleton<IMyService, MyService>();
    

    Then the service needs some configuration passing into the constructor

     public MyService( IOptions<MyConfig> config)
    

    Then I have config in appsettings.json

      "MyConfig": {
        "Username": "",
        "Password": "",
        "API": "",
        "Timeout": 6000
      },
    

    How do you get dependency injection to pass in the config section?

    I'm sure it's really obvious but complete .net core newbie.

    Thanks

  • AbsolutelyN 49 posts 331 karma points
    Oct 22, 2021 @ 12:55
    AbsolutelyN
    0

    I think I've figured it - missing registering the configuration class.

    builder.Services.Configure<MyConfig>(builder.Config.GetSection("MyConfig"));
    
  • Kevin Jump 1997 posts 12599 karma points MVP 5x c-trib
    Oct 22, 2021 @ 12:58
    Kevin Jump
    101

    Hi,

    if you have a class that represents your options.

    eg.

    public class MyConfigClass {
       public string Username {get;set }
       public string Password {get;set;}
       public string API {get;set;}
       public int TimeOut {get;set;} = 6000; 
    }
    

    You can register your options in a composer

    var options = builder.Services.AddOptions<MyConfigClass>()
                .Bind(builder.Config.GetSection("MyConfig"));
    

    "MyConfig" is the name of the section in appsettings.

    then you should be able to inject this as an IOptions<MyConfigClass> into any services/controllers that need to read your settings

    there are other options such as IMontorOptions that mean you don't have to restart to update options. Microsoft docs has a bit more on them,.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0

  • AbsolutelyN 49 posts 331 karma points
    Oct 22, 2021 @ 13:10
    AbsolutelyN
    0

    Thank you Kevin - I did get it working just before you replied but will use your version. Hopefully the thread will help anyone else new to v9 not familiar with this. Much appreciated.

  • Thomas 247 posts 517 karma points c-trib
    20 days ago
    Thomas
    0

    How would yuo access data from the appsettings in razor view?

    Have tried to Inject my custom Settings models but is not working..

  • Thomas 247 posts 517 karma points c-trib
    20 days ago
    Thomas
    0
    @inject Microsoft.Extensions.Options.IOptions<PlatformSettings> Options
    @{
       var _platformSettings = Options.Value;
     }
    
Please Sign in or register to post replies

Write your reply to:

Draft