17

I have an application currently in Azure and whenever we push it into the Staging segment, we cannot truly test since the connection string is pointing to the prod database.

Someone mentioned to me that you should be able to set the connection string in the ServiceConfiguration.cscfg file instead (or with) the web.config file. That way you can change the connection string in the Azure portal instead of republishing a who app.

Does anyone know how to do this?

abatishchev
  • 92,232
  • 78
  • 284
  • 421
Andy
  • 173
  • 1
  • 4

3 Answers3

26

In your ServiceConfiguration.cscfg file add:

<ServiceConfiguration ... />
  <Role ... />
    <ConfigurationSettings>
      <Setting name="DatabaseConnectionString" value="put your connection string here" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

Now you have a connection string you can change by editing the configuration inside the azure portal.

Then anytime you need to retrieve the connection string you can do it using:

using Microsoft.WindowsAzure.ServiceRuntime;

...

String connString = RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString")

You may need to add Microsoft.WindowsAzure.ServiceRuntime.dll to your references.

RoleEnviroment.IsAvailable can be used to test if your are running in Azure, and if not to fall back to your web.config settings.

using System.Configuration;
using Microsoft.WindowsAzure.ServiceRuntime;

...

if (RoleEnvironment.IsAvailable)
{
    return RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString");
}
else
{
    return ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; 
}

This article has a more verbose explanation of the above.

Adrian Toman
  • 10,902
  • 5
  • 44
  • 60
David Steele
  • 3,223
  • 17
  • 21
  • 1
    This doesn't allow the specification of a connection provider (required by Entity Framework), such as System.Data.EntityClient. Any thoughts? – Dale Anderson Feb 28 '12 at 07:12
  • @DaleAnderson, put them in web.config. Can't think of a find a better place that works with EF :( – CMircea Apr 07 '12 at 13:55
  • Yeh. I actually find web.config is best for that sort of stuff anyway - doesn't require a redeploy to change it in a testing scenario and can be managed easily using config transforms. – Dale Anderson Apr 08 '12 at 04:31
  • I added `Microsoft.WindowsAzure.ServiceRuntime`, but I get an exception `Could not load file or assembly 'msshrtmi, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies`. Apparently, `Microsoft.WindowsAzure.ServiceRuntime` depends on that file. Any ideas? – cheesus Jun 23 '12 at 14:00
  • You cannot use RoleEnvironment.IsAvailable apparently in non-Azure environments. On MSDN it says that the issue may be fixed at some point in the future. – Sergey Barskiy Jul 09 '12 at 20:13
  • 4
    There is a Microsoft nuget package for this named "Microsoft.WindowsAzure.ConfigurationManager" that solves this problem. Read more here: http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.cloudconfigurationmanager.aspx – angularsen Sep 09 '12 at 10:02
3

For Entity Framework, you do not need to provide a providerName, it's already inside in the connectionstring. The reason why it does not work when it's in azure settings is, it contains &quot symbol which needs to be transalated back to " before creating a new EntityConnection. You can do it using HttpUtility.HtmlDecode in System.Web.

fionbio
  • 3,108
  • 1
  • 19
  • 34
Sandy
  • 31
  • 1
1

Basically you need to define these setting in Azure service configuration file. Check here. Once defined these can be changed from Azure portal.

Chandermani
  • 42,177
  • 11
  • 82
  • 86