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

查看:113
本文介绍了检索和使用的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 方法:

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

下面是结果:

名称:LocalSqlServer,ConnectionString的:数据源= \\ SQLEX $ P $干燥综合征;集成安全性= SSPI; AttachDBFilename = | DataDirectory目录| ASPNETDB.MDF;用户实例=真

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

我试过上面 ConfigurationManager.ConnectionStrings ,以及与服务器(天青)连接字符串都没有了。

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.

当你在本地开发,你可能使用位于本地主机\\ SQLEx preSS或类似的数据库。如果部署,而不必设立的web.config转型这将意味着,在Windows Azure上运行你的网站仍然将指向到localhost \\ SQLEx preSS,这是不是你想要的东西。

When you're developing locally, you probably use a database located in localhost\SQLExpress 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 localhost\SQLExpress, 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(的来源),它这样做在运行时。因此,为了检查哪个的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天全站免登陆