0

I am building an Azure Function and have a config that looks like this

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsSecretStorageType": "files",
    "SQLAZURECONNSTR_SqlConnection": "yyyy",
    "SqlConnection": "zzzzz",
    "QueueStorage": "xxxxxx"

  },
  "ErpConfiguration": {
    "BaseUri": "https://localhost:7209/",
    "CompanyDetail": {
      "DaysInPastToCollect": 1,
      "GetCount": {
        "ItemsPerPage": 50,
        "Endpoint": "xxxx/count"
      },
      "GetList": {
        "Format": "json",
        "StorageQueueName": "local-emulator",
        "Endpoint": "xxxx",
        "UrlFilters": {
          "UseLastUpdatedDateFilter": true,
          "UseFormatFilter": true,
          "UsePaginationFilters": true,
          "SelectedColumns": "CompCode, Lastupdateddate"
        }
      }
    }
  }
}

In Startup.cs I have a method that should be mapping like this

public static void AddConfig(this IServiceCollection services)
{
    services
        .AddOptions<ErpConfiguration>()
        .Configure<IConfiguration>((settings, configuration) => { configuration.Bind("ErpConfiguration", settings); });
}

However, when running this and checking the config, in the service that looks like this

public CompanyCodeService(
    IOptions<ErpConfiguration> erpConfiguration,
    HttpClient client)
{
    _erpConfiguration = erpConfiguration.Value;
    _client = client;

    _serializerSettings = new JsonSerializerSettings();
    _serializerSettings.Converters.Add(new IsoDateTimeConverter());

    _client.BaseAddress = _erpConfiguration.BaseUri;
}

The properties in erpConfiguration are null or set as the default values in the class.

I would be grateful if someone could tell me where I am going wrong and why the values are not being mapped correctly.

I have looked in places such as Read values from local.settings.json in VS 2017 Azure Function development

https://medium.com/@aranmulholland/injecting-application-settings-into-azure-functions-using-ioptions-f9ca07ac80ce

Add custom section to .NET Core local.settings.json

Ideally I dont want to have a pointer to my configs as when this is deployed this is coming from Azure App Configs

Simon Price
  • 3,011
  • 3
  • 34
  • 98
  • 2
    If I'm not mistaken, you need to move ErpConfiguration inside the Values property. That's how this should be formatted, it's different from your traditional appsettings.json. Keys should be like Values --> ErpConfiguration:BaseUri: "your value" – lopezbertoni Jun 28 '22 at 12:22
  • 1
    @lopezbertoni I have found the answer, but youre close – Simon Price Jun 28 '22 at 12:59
  • Nice, another approach to managing this is to use Azure App Configuration. If you have many config entries, this can be a pain to configure in Azure. – lopezbertoni Jun 28 '22 at 13:17

1 Answers1

1

The way this has been solved is as LopexBertoni suggested, it needed to be moved, but it also only appears to work like this

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsSecretStorageType": "files",
    "SQLAZURECONNSTR_SqlConnection": "yyy",
    "SqlConnection": "zzz",
    "QueueStorage": "xxx",
    "ErpConfiguration:BaseUri": "https://localhost:7209/",
    "ErpConfiguration:CompanyDetail:DaysInPastToCollect": 1,
    "ErpConfiguration:CompanyDetail:GetCount:ItemsPerPage": 50,
    "ErpConfiguration:CompanyDetail:GetCount:Endpoint": "removed",
    "ErpConfiguration:CompanyDetail:GetList:Endpoint": "removed",
    "ErpConfiguration:CompanyDetail:GetList:Format": "json",
    "ErpConfiguration:CompanyDetail:GetList:StorageQueueName": "local-emulator",
    "ErpConfiguration:CompanyDetail:GetList:UrlFilters:UseLastUpdatedDateFilter": true,
    "ErpConfiguration:CompanyDetail:GetList:UrlFilters:UseFormatFilter": true,
    "ErpConfiguration:CompanyDetail:GetList:UrlFilters:UsePaginationFilters": true,
    "ErpConfiguration:CompanyDetail:GetList:UrlFilters:SelectedColumns": "CompCode, Lastupdateddate"
  }
}

It makes sense in one way because this is how it will sit in Azure App Configurations, but I thought this should sit as nested json objects as per the question

Simon Price
  • 3,011
  • 3
  • 34
  • 98