在服务配置中设置应用程序连接字符串,而不是在Azure中设置web.config

在服务配置中设置应用程序连接字符串,而不是在Azure中设置web.config,azure,web-config,connection-string,azure-cloud-services,azure-configuration,Azure,Web Config,Connection String,Azure Cloud Services,Azure Configuration,我目前在Azure中有一个应用程序,无论何时我们将其推送到暂存段,我们都无法真正进行测试,因为连接字符串指向prod数据库 有人向我提到,您应该能够在ServiceConfiguration.cscfg文件中设置连接字符串,而不是(或使用)web.config文件。这样,您就可以在Azure门户中更改连接字符串,而不是重新发布who应用程序 有人知道怎么做吗?基本上,您需要在Azure服务配置文件中定义这些设置。检查。定义后,可以从Azure portal更改这些内容。在ServiceConfi

我目前在Azure中有一个应用程序,无论何时我们将其推送到暂存段,我们都无法真正进行测试,因为连接字符串指向prod数据库

有人向我提到,您应该能够在ServiceConfiguration.cscfg文件中设置连接字符串,而不是(或使用)web.config文件。这样,您就可以在Azure门户中更改连接字符串,而不是重新发布who应用程序


有人知道怎么做吗?

基本上,您需要在Azure服务配置文件中定义这些设置。检查。定义后,可以从Azure portal更改这些内容。

在ServiceConfiguration.cscfg文件中添加:

<ServiceConfiguration ... />
  <Role ... />
    <ConfigurationSettings>
      <Setting name="DatabaseConnectionString" value="put your connection string here" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>
您可能需要将Microsoft.WindowsAzure.ServiceRuntime.dll添加到引用中

RoleEnviroment.IsAvailable
可用于测试您的应用程序是否在Azure中运行,以及是否返回到您的web.config设置

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

...

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

对于实体框架,您不需要提供providerName,它已经在connectionstring中。当它在azure设置中时不起作用的原因是,它包含“需要在创建新的EntityConnection之前转换回的符号”。您可以使用System.Web中的HttpUtility.HtmlCode来执行此操作。

这不允许指定连接提供程序(实体框架要求),例如System.Data.EntityClient。有什么想法吗?@DaleAnderson,将它们放在web.config中。想不出更好的方法来找到与EF一起工作的地方:(是的。事实上,我发现web.config最适合这种类型的东西——在测试场景中不需要重新部署就可以更改它,并且可以使用配置转换轻松地进行管理。我添加了
Microsoft.WindowsAzure.servicereruntime
,但我遇到了一个异常
无法加载文件或程序集的msshrtmi,版本=1.7.0.0,区域性=中立),PublicKeyToken=31bf3856ad364e35'或其依赖项之一
。显然,
Microsoft.WindowsAzure.ServiceRuntime
依赖于该文件。有什么想法吗?有一个名为“Microsoft.WindowsAzure.ConfigurationManager”的Microsoft nuget软件包可以解决此问题。请在此处阅读更多信息:
using System.Configuration;
using Microsoft.WindowsAzure.ServiceRuntime;

...

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