为Webhooks

时间:2017-05-25 12:05:09

标签: azure asp.net-web-api azure-table-storage asp.net-webhooks

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

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

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

我删除了" MS_AzureStoreConnectionString"在我的web.config中输入密钥。

我试图将azure表存储连接字符串注入到我的启动类中的默认Web挂钩实现中。

SettingsDictionary settings = new SettingsDictionary();

string connectionString = helper.getTableSrorageConnectionString();

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

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

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

  

请提供名为' MS_AzureStoreConnectionString'的Microsoft Azure存储连接字符串。在' Web.Config'的配置字符串部分中文件。

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

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

请提出可行的解决方案。

2 个答案:

答案 0 :(得分:0)

根据您的描述,如果要在代码中设置连接字符串。我建议你可以在WebApiConfig类中创建自己的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;
        }


    }

答案 1 :(得分:0)

下面的代码片段也适用于我。你必须在config.InitializeCustomWebHooksAzureStorage()

之前添加以下行。

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