将服务配置中的应用程序ConnectionString设置为Azure中的web.config

前端之家收集整理的这篇文章主要介绍了将服务配置中的应用程序ConnectionString设置为Azure中的web.config前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前在Azure中有一个应用程序,每当我们将其推入Staging段时,我们无法真正测试,因为连接字符串指向prod数据库

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

有谁知道如何做到这一点?

解决方法

在您的ServiceConfiguration.cscfg文件添加
<ServiceConfiguration ... />
  <Role ... />
    <ConfigurationSettings>
      <Setting name="DatabaseConnectionString" value="put your connection string here" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

现在您可以通过编辑天蓝门户网站中的配置来更改连接字符串。

然后任何时候你需要检索连接字符串,你可以使用:

using Microsoft.WindowsAzure.ServiceRuntime;

...

String connString = RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString")

您可能需要将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; 
}

This article has a more verbose explanation of the above.

原文链接:https://www.f2er.com/html/233302.html

猜你在找的HTML相关文章