I am trying to use Azure Appconfiguration in one of my functions but I am not fully understanding why it does not work, I get the object ref error when trying to declare the CosmosClient, it seems as it cannot read the appSettings object for some reason
the relevant code looks as follows
public class BarCodeScanV4
{
//
// Read basic settings from the local APP Service Configurations
// This uses local.settings.json when ran locally
//
// NOTE: V4 and onwards of this function can NOT be run locally
// because it uses managed identity to access the global configuration store
//
private static readonly IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
private static readonly string envLabel = config["envLabel"];
private static readonly string configurationStorePrimaryEndpoint = config["configurationStorePrimaryEndpoint"];
//
// End of read basic settings
//
//
// Read global settings from Azure APP Configuration
// This does NOT use local.settings.json so the settings must exist in Azure even when ran locally
//
private static IConfiguration Configuration { set; get; }
private static IConfigurationRefresher ConfigurationRefresher { set; get; }
private static Settings appSettings { get; set; }
static BarCodeScanV4()
{
var builder = new ConfigurationBuilder();
builder.AddAzureAppConfiguration(options =>
{
options
.Connect(new Uri(configurationStorePrimaryEndpoint), new ManagedIdentityCredential())
.Select(KeyFilter.Any, envLabel)
.ConfigureRefresh(refreshOptions =>
refreshOptions.Register("SLAPI:Settings:ConfigurationStoreVersion", refreshAll: true)
.SetCacheExpiration(TimeSpan.FromSeconds(300))
);
ConfigurationRefresher = options.GetRefresher();
});
Configuration = builder.Build();
//
// Use helper class to fetch the global settings
// NOTE: This is not used at runtime, this is settings used by singletons etc
// runtime settings are added below in the actual function
//
appSettings = Utils.AppSettings.GetAppSettingsAsync(appSettings, Configuration);
//
// End of fetching global settings
//
}
//
// End of read global settings
//
//
// Create the clients outside of the function to ensure they are reused and not re created for every call to the function
// this is CRITICAL for performance and avoid running out of ports on the connections.
//
private static CosmosClient cosmosClient = new CosmosClient(appSettings.CosmosDBEndpoint, appSettings.CosmosDBPrimaryKey, new CosmosClientOptions() { AllowBulkExecution = false, ConnectionMode = ConnectionMode.Direct });
private static EventHubProducerClient eventHubClient = new EventHubProducerClient(appSettings.EventHubConnectionString, appSettings.EventHubName);
private static Database database = null;
private static Container usersContainer = null;
private static Container slapiAuthContainer = null;
private static Container slapiFormattedDataContainer = null;
private static Container promosContainer = null;