Home > database >  Loading child settings configuration settings into DI not loading list
Loading child settings configuration settings into DI not loading list

Time:11-04

Here is the section in question from my appsettings.json

"AzureServiceBus": {
    "ConnectionString":  "XXXX",
    "Queues": {
      "Name": "DummyMessageQueue",
      "Pub" : true,
      "Sub" : true
    }
  },

The issue is that _serviceBusConfiguration.Quotes is always null. Its not loading the list.

Program.cs

In my program.cs I load it as follows

// Register configuration options for the azure service bus in a options object. 
builder.Services.Configure<ServiceBusConfigurationOptions>(
    builder.Configuration.GetSection("AzureServiceBus"));

I have tried adding GetChildren but this just results in compile error.

builder.Services.Configure<ServiceBusConfigurationOptions>(builder.Configuration.GetSection("AzureServiceBus").GetChildren<List<Queue>>());

Also didnt help

builder.Services.Configure<ServiceBusConfigurationOptions>(options => builder.Configuration.GetSection("AzureServiceBus").Bind(options));

Also didnt work

var x = builder.Configuration.GetSection("AzureServiceBus");
x.GetChildren();
builder.Services.Configure<ServiceBusConfigurationOptions>(x);

Di method

The constructor in my service then loads it as follows

public MessageReceiver(ILogger<MessageReceiver<T>> logger,
        IOptions<ServiceBusConfigurationOptions> serviceBusConfiguration)
    {
        _logger = logger;
        _serviceBusConfiguration = serviceBusConfiguration.Value;

    }

The issue is that _serviceBusConfiguration.Quotes is always null. Its not loading the list.

classes

public class ServiceBusConfigurationOptions
{
    public string ConnectionString { get; set; }
    public string QueueName { get; set; }
    public List<Queue> Queues { get; set; }
}

public class Queue
{
    public string Name { get; set; }
    public bool Sub { get; set; }
    public bool Pub { get; set; }
}

CodePudding user response:

You configuration model:

public class ServiceBusConfigurationOptions
{
    public string ConnectionString { get; set; }
    public string QueueName { get; set; }
    public List<Queue> Queues { get; set; }
}

Queues is a list, but in your appsetting.json, the "Queues" section is not array, this caused configuration binding failed. Try to change the "AzureServiceBus" section to:

"AzureServiceBus": {
    "ConnectionString":  "XXXX",
    "Queues": [{
      "Name": "DummyMessageQueue",
      "Pub" : true,
      "Sub" : true
    }]
  },
  • Related