为 Webhooks 注入 azure table 存储连接字符串

Injecting the azure table storage connection string for Webhooks

我正在为我们的 webhook 发件人服务之一使用 azure table 存储。

为此,必须在 web.config 文件中设置 MS_AzureStoreConnectionString。

现在我需要从密钥保管库中获取上述值,这只能通过自定义实现来完成。

我删除了 web.config 中的 "MS_AzureStoreConnectionString" 键。

并且我已经尝试在我的启动 class.

中将 azure table 存储连接字符串注入到默认的 Web 挂钩实现中,如下所示
SettingsDictionary settings = new SettingsDictionary();

string connectionString = helper.getTableSrorageConnectionString();

ConnectionSettings connection = new ConnectionSettings("MS_AzureStoreConnectionString", connectionString);

settings.Connections.Add("MS_AzureStoreConnectionString", connection);

但是我在 运行 我的 APP 中遇到了以下问题。

Please provide a Microsoft Azure Storage connection string with name 'MS_AzureStoreConnectionString' in the configuration string section of the 'Web.Config' file.

我不想在 web.config/app 设置中保留连接字符串。

如何将连接字符串注入默认的 Web 挂钩实现?

请就此提出可能的解决方案。

根据你的描述,如果你想在代码中设置连接字符串。我建议您可以在 WebApiConfig class.

中创建自己的 InitializeCustomWebHooksAzureStorage 方法

在 InitializeCustomWebHooksAzureStorage 获取到 SettingsDictionary 之后,就可以将连接字符串添加到 SettingsDictionary 中了。

更多详情,您可以参考以下代码:

  public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.InitializeCustomWebHooks();
            // Change the method
            config.InitializeCustomWebHooksAzureStorage2();
            config.InitializeCustomWebHooksApis();
            config.InitializeReceiveCustomWebHooks();
        }

        public static void InitializeCustomWebHooksAzureStorage2(this HttpConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            WebHooksConfig.Initialize(config);
            ILogger logger = config.DependencyResolver.GetLogger();
            SettingsDictionary settings = config.DependencyResolver.GetSettings();
            settings.Add("MS_AzureStoreConnectionString", "connection string");
            IStorageManager storageManager =  GetInstance2(logger);
            IWebHookStore store;           
            store = new AzureWebHookStore(storageManager, settings, logger);           
            CustomServices.SetStore(store);
        }


        private static IStorageManager _storageManager;

        internal static IStorageManager GetInstance2(ILogger logger)
        {
            if (_storageManager != null)
            {
                return _storageManager;
            }

            IStorageManager instance = new StorageManager(logger);
            Interlocked.CompareExchange(ref _storageManager, instance, null);
            return _storageManager;
        }


    }

下面的代码片段也对我有用。您必须在 config.InitializeCustomWebHooksAzureStorage()

之前添加以下行

ConnectionSettings 值 = new ConnectionSettings("MS_AzureStoreConnectionString", "connectionString")
SettingsDictionary设置=config.DependencyResolver.GetSettings(); settings.Add("MS_AzureStoreConnectionString",值);