Search code examples
.netazureazure-functionsazure-webjobsazure-webjobssdk

Is GetConnectionStringOrSetting deprecated? What is the best replacement?


In the past developing a Timer Triggered Azure Function I've used GetConnectionStringOrSetting from Microsoft.Extensions.Configuration to load connection strings, secrets and regular settings. It didn't matter where they lived, it would find them.

But in migrating to .NET 8 I'm not sure I should anymore? Looks like it depends on .NET standard and it no longer works with Microsoft.Extensions.Configuration.UserSecrets to load secrets from a secure folder when developing locally.


Solution

  • public static class IConfigurationExtensions
    {        
        /// <summary>
        /// Looks for a connection string by first checking the ConfigurationStrings section, and then the root.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="connectionName">The connection string key.</param>
        /// <returns></returns>
        public static string GetConnectionStringOrSetting(this IConfiguration configuration, string connectionName) =>
            configuration.GetConnectionString(connectionName) ?? configuration[connectionName];
    }
    

    I just ended up stealing that one static function so I wouldn't have to re-write all the calls to it.