Copied to clipboard

Flag this post as spam?

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


  • AbsolutelyN 89 posts 437 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 89 posts 437 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 2348 posts 14896 karma points MVP 8x c-trib
    Oct 22, 2021 @ 12:58
    Kevin Jump
    106

    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 89 posts 437 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 319 posts 606 karma points c-trib
    Nov 18, 2021 @ 10:37
    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 319 posts 606 karma points c-trib
    Nov 18, 2021 @ 10:52
    Thomas
    1
    @inject Microsoft.Extensions.Options.IOptions<PlatformSettings> Options
    @{
       var _platformSettings = Options.Value;
     }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies