简体   繁体   中英

Injecting the azure table storage connection string for Webhooks

I am using the azure table storage for the one of our webhook sender service.

For this, the MS_AzureStoreConnectionString has to be set in the web.config file.

Now i need to get the above values from the key vault which can be done through custom implementation only.

I removed the "MS_AzureStoreConnectionString" key in my web.config.

And i have tried to inject the azure table storage connection string to the default web hook implementation as below in my startup class.

SettingsDictionary settings = new SettingsDictionary();

string connectionString = helper.getTableSrorageConnectionString();

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

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

But I am facing the below issue while running my APP.

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

i don't want to persist the connection string in the web.config / app settings.

How can i inject the connection string to the default web hook implementation?

Kindly suggest the possible solution on this.

According to your description, if you want to set the connection string in the codes. I suggest you could create your own InitializeCustomWebHooksAzureStorage method in the WebApiConfig class.

After the InitializeCustomWebHooksAzureStorage get the SettingsDictionary, then you could add the connection string into the SettingsDictionary.

More details, you could refer to below codes:

  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;
        }


    }

The below snippet also worked for me. you have to add the below lines before config.InitializeCustomWebHooksAzureStorage()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM