Search code examples
umbraco7azure-web-app-serviceazure-deployment-slots

Umbraco, Azure deployment slots and Connection Strings


We're trying to leverage Azures deployment slots for an Umbraco site we've built.

By default Umbraco uses a DSN defined in the connectionStrings sections of the web.config and we want it to use the connection string for the deployment slot it's sitting in.

What we've tried

Azure deployment slots put all defined app settings (and connection strings) into environment variables and to access them we can use Environment.GetEnvironmentVariable() which works but there doesn't seem to be a way to tell Umbraco to do this.

So in OnApplicationInitialized() (in /App_Code/Core/UmbracoAppStart.cs) we loaded the connectionstring section from the web.config, grabbed the connstr from env vars, added the DSN to the connectionstring section and saved. The correct connection string is grabbed and stored but this seems to recycle the app (due to a web.config change) and thus we just get timeouts. (Or Umbraco XML cache errors, or it takes 20 mins to load the page).

I know you can store the appsettings and connectionstrings sections in separate files. But the file attribute (that doesn't cause a recycle if the referenced file is changed) doesn't work on the connectionStrings section - only the configSource attribute and that DOES recycle if changed.

(from: ASP.NET web.config: configSource vs. file attributes)

Help

Has anyone found a way around this? We simply need to get Umbraco to use the deployment slot connection string - not the one in webconfig.

I'm even willing to copy and paste blindly at the moment without understanding how it works - and I hate doing that :). But that's what happens when people agree when the client wants to go live just before Christmas...


Solution

  • You don't need to do any code to use Azure connection string or the app settings. Just give them the same keys/names as you have on your web.config and they will be used instead.

    So if you have this on your web config:

    <add name="umbracoDbDSN" connectionString="Server={server};Initial Catalog={db};Persist Security Info=False;User ID={user};Password={password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=300;" providerName="System.Data.SqlClient" />
    

    Your slot configuration should be this:

    enter image description here

    To replace an app setting just use the same key. So for this:

    <add key="umbracoUseSSL" value="false" />
    

    You'd use this: enter image description here

    If you want the setting to be slot specific you have to activate the Slot setting checkbox.