How to load appsetting.json section into Dictionary in .NET Core?

后端 未结 10 1514
误落风尘
误落风尘 2020-12-05 16:49

I am familiar w/ loading an appsettings.json section into a strongly typed object in .NET Core startup.cs. For example:

public class CustomSection 
{
   publ         


        
10条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 17:37

    I use the way below:

    appsettings.json:

      "services": {
          "user-service": "http://user-service:5000/",
          "app-service": "http://app-service:5000/"
      } 
    

    startup.cs:

      services.Configure>(Configuration.GetSection("services"));
    

    Usage:

    private readonly Dictionary _services;
    public YourConstructor(IOptions> servicesAccessor)
    {
        _services = servicesAccessor.Value;
    }
    

提交回复
热议问题