CyclingFreak
CyclingFreak

Reputation: 1625

Strongly Typed Configuration Settings not mapped

I am trying to setup a strongly typed configuration setting in my ASP.NET Core 2.1 application.

In my startup.cs file I have the following code:

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

My setting class AzureStorageConfig is looking like this:

public class AzureStorageConfig
{
    public string StorageConnectionString { get; internal set; }
}

My appSetting.json:

{
 "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
    }
 },
 "AzureStorageConfig": {
    "StorageConnectionString": "UseDevelopmentStorage=true"
 }
}

When I run the code, my AzureStorageConfig.StorageConnectionString is always null.

If I set a breakpoint in the startup.cs file, I can see that the settings are there:

enter image description here

But when injecting the AzureStorageConfig, it's null.

public class RestaurantService : IRestaurantService
{
    private AzureStorageConfig m_Config;

    public RestaurantService(IOptions<AzureStorageConfig> config)
    {
        m_Config = config.Value;
    }
}

enter image description here

I think I have everything like described here: https://weblog.west-wind.com/posts/2016/May/23/Strongly-Typed-Configuration-Settings-in-ASPNET-Core

Upvotes: 3

Views: 162

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93153

Your StorageConnectionString property has an internal setter, but this will need to be public in order for the binder to use it when attempting to bind from your Configuration instance:

public class AzureStorageConfig
{
    public string StorageConnectionString { get; set; }
}

Upvotes: 2

Related Questions