检索和使用 Windows Azure 的连接字符串? [英] Retrieve and use Windows Azure's connection strings?

查看:20
本文介绍了检索和使用 Windows Azure 的连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Azure 管理门户配置->连接字符串(链接资源)中配置了连接字符串:

I've configured connection strings in Azure management portal Configure->Connection Strings (linked resources):

这些连接字符串有什么用?

What are these connection strings useful for?

我尝试删除连接.来自 web.config 文件的字符串,所以它应该从这里读取,但它没有.

I tried deleting the conn. strings from web.config file, so it should read from here, but it doesn't.

还有什么办法吗?

基本上我希望这些连接字符串覆盖 web.config 中的连接字符串以用于生产环境.

Basically I want these connection strings to override the connection strings in web.config to be used in production environment.

我已将以下内容添加到 Application_Start 方法中:

I've added the following to the Application_Start method:

var sb = new StringBuilder();
var appConfig = ConfigurationManager.OpenMachineConfiguration();  
foreach (ConnectionStringSettings conStr in appConfig.ConnectionStrings.ConnectionStrings)
  sb.AppendFormat("Name: {0}, ConnectionString: {1}
", conStr.Name, conStr.ConnectionString);
throw new Exception(sb.ToString());

结果如下:

名称:LocalSqlServer,ConnectionString:数据源=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

Name: LocalSqlServer, ConnectionString: data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

我也用 ConfigurationManager.ConnectionStrings 尝试了上述方法,但服务器 (Azure) 连接字符串不存在.

I tried the above with ConfigurationManager.ConnectionStrings as well and the server (Azure) connection strings were not there.

推荐答案

门户中的连接字符串允许您覆盖 web.config 中定义的连接字符串.

The connection strings in the Portal allow you to override the connection strings defined in the web.config.

当您在本地进行开发时,您可能会使用位于 localhostSQLExpress 或类似位置的数据库.如果您在未设置 web.config 转换的情况下进行部署,则意味着您在 Windows Azure 中运行的网站仍将指向 localhostSQLExpress,这不是您想要的.

When you're developing locally, you probably use a database located in localhostSQLExpress or something similar. If you deploy without having set up web.config transformation it would mean that your Web Site running in Windows Azure would still point to localhostSQLExpress, which isn't something you would want.

门户中的连接字符串允许您覆盖已在 web.config 中定义的现有连接字符串.如果您的 web.config 不包含与门户中配置的连接字符串同名的连接字符串,则不会在运行时添加和访问它.这可能是您遇到的问题.

The connection strings in the Portal allow you to override existing connection strings which are already defined in the web.config. If your web.config does not contain a connection string with the same name as the one configured in the portal, it will not be added and be accessible at runtime. This might be the issue you're experiencing.

要解决此问题,只需在您的 web.config 文件中添加一个与您已添加到门户中的名称相同的连接字符串即可.

To fix this, simply add a connection string to your web.config file with the same name as the one you have already added to the portal.

更新:就像我在评论中已经解释过的那样,Windows Azure 网站不会物理修改 web.config (source),它在运行时执行此操作.因此,为了检查哪些 AppSettings 和 ConnectionStrings 在运行时实际可用,请尝试以下操作:

Update: Like I already explained in a comment, Windows Azure Web Sites does not physically modify the web.config (source), it does this at runtime. So in order to check which AppSettings and ConnectionStrings are actually available at runtime, try this:

控制器:

public ActionResult Index()
{
  ViewBag.ConnectionStrings =
    ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>();
  ViewBag.AppSettings = ConfigurationManager.AppSettings;
  return View();
}

查看:

<h3>ConnectionStrings:</h3>
<ul>
  @foreach (var setting in ViewBag.ConnectionStrings)
  {
    <li>@setting.Name: @setting.ConnectionString</li>
  }
</ul>
<h3>AppSettings:</h3>
<ul>
  @foreach (var s in ViewBag.AppSettings)
  {
    <li>@setting: @System.Configuration.ConfigurationManager.AppSettings[s]</li>
  }
</ul>

这篇关于检索和使用 Windows Azure 的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆