为什么我无法在此天蓝色功能中读取应用程序设置?

时间:2020-08-17 03:10:35

标签: c# azure-functions

我正在尝试在我的一项功能中使用Azure Appconfiguration,但我不完全理解为什么它不起作用,尝试声明CosmosClient时出现对象引用错误,似乎无法读取appSettings对象某些原因

相关代码如下

  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;

0 个答案:

没有答案
相关问题