Have EPiServer 7 pick up connection strings defined in Azure Portal

As you may have noticed EPiServer does not pick up the connection strings defined in Azure. This has been fixed  in 8.30, but some of our projects have not been upgraded yet and are still on 7.15+, so this is what you can do to fix that, if you run into the same issue.

You can implement your own DatabaseFactory. To have it pick up the Azure configured string you only need to use the ConnectionManager from .Net to get the connection string, which does pick up the ones configured in Azure, and replace the one in the ConnectionStringSettings with this one. See David’s article how to let EPiServer know to use this factory.

    public class AzureDatabaseFactory : SqlDatabaseFactory, IDatabaseFactory
    {
         public new IDatabaseHandler CreateDefaultHandler()
        {
            SiteDataSettingsElement currentSiteSettings = this.GetCurrentSiteSettings();
            
                        ConnectionStringSettings connectionStringSettings = EPiServerDataStoreSection.ConfigurationInstance.ConnectionStrings.ConnectionStrings[currentSiteSettings.ConnectionStringName];

            if (connectionStringSettings == null)
            {
                throw new ConfigurationErrorsException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "No connection string found with the configured name '{0}'.",
                        currentSiteSettings.ConnectionStringName));
            }
            
                        connectionStringSettings.ConnectionString = ConfigurationManager.ConnectionStrings[currentSiteSettings.ConnectionStringName].ConnectionString;

            return new SqlDatabaseHandler(
                connectionStringSettings,
                currentSiteSettings.Retries,
                currentSiteSettings.RetryDelay,
                currentSiteSettings.DatabaseQueryTimeout);
        }

                public new IDatabaseHandler CreateHandler(ConnectionStringSettings connectionString)
        {
            SiteDataSettingsElement currentSiteSettings = this.GetCurrentSiteSettings();
            
            connectionString.ConnectionString = ConfigurationManager.ConnectionStrings[currentSiteSettings.ConnectionStringName].ConnectionString;
            return base.CreateHandler(connectionString);
        }
    }

Hope this helps.

Leave a comment