Azure 连接字符串最佳实践
我有一个刚刚迁移到 Azure 的应用程序。目前,我使用 web.config 转换来管理更改数据库连接字符串 dev/staging/prod 环境。在 Azure 中如何最好地管理这些多个连接字符串?
I have an application that I am just migrating to Azure. Currently I use web.config transformation to manage changing the database connecting string dev/staging/prod environments. How is it best to manage these multiple connection strings in Azure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果开发人员是否可以看到生产凭据并不重要,您可以使用内置的 Visual Studio 10 配置转换。如果这是您正在寻找的内容,请按照下列步骤操作:
1.导航到文件资源管理器中的 Azure 项目文件夹.cscfg 文件。在这些文件中,您可以使用普通的配置转换语法
2. 复制 ServiceConfiguration.cscfg
3. 将副本重命名为 ServiceConfiguration.Base.cscfg
4. 对于每个构建配置(例如 Dev、Staging、Production),创建 ServiceConfiguration.
5. 在文本编辑器中打开 .ccproj 文件
6. 找到以下节点,
并将其替换为以下节点(您必须编辑此块以匹配您的构建配置):
7. 在 .ccproj 文件末尾添加以下内容,就在
上方;
:8.如果您使用的 CI 服务器未安装 Visual Studio 10,则可能需要复制 C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\ Web 文件夹及其内容从开发计算机传输到服务器。
更新:正如@SolarSteve指出的,您可能必须将命名空间添加到 ServiceConfiguration.*.cscfg 文件中。以下是 ServiceConfiguration.Base.cscfg 的示例:
In cases where it doesn't matter if the developer can see production credentials, you can use the built-in Visual Studio 10 config transformations. If this is what you're looking for, follow these steps:
1.Navigate to your Azure project folder in file explorer
2. Make a copy of ServiceConfiguration.cscfg
3. Rename copy to ServiceConfiguration.Base.cscfg
4. For each build configuration (e.g. Dev, Staging, Production), create a ServiceConfiguration.<build config name>.cscfg file. In these files, you can use the normal config transformation syntax
5. Open your .ccproj file in a text editor
6. Find the following node,
and replace it with this (you will have to edit this block to match your build configs):
7.Add the following at the end of the .ccproj file, just above
</Project>
:8.If you're using a CI server that doesn't have Visual Studio 10 installed, you'll probably have to copy the C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\Web folder and its contents from a development machine to the server.
Update: As @SolarSteve noted, you might have to add a namespace to your ServiceConfiguration.*.cscfg files. Here's an example of ServiceConfiguration.Base.cscfg:
就我们个人而言:
有关扫描应用程序设置和云环境以获取配置值的设置管理类的示例,您可以查看开源 Lokad.CQRS对于 Windows Azure 项目(请参阅 CloudSettingsProvider)
Personally we:
For sample of the settings management class that scans application settings and cloud environment for configuration values, you can check out open source Lokad.CQRS for Windows Azure project (see CloudSettingsProvider)
您可以在 Azure SDK 1.7 中使用 CloudConfigurationManager http://msdn.microsoft.com/en-us/ LIBRARY/microsoft.windowsazure.cloudconfigurationmanager
首先在 ServiceConfiguration.cscfg(例如 ServiceConfiguration.Cloud.cscfg)中查找配置设置。如果不存在,则会回退到 web.config 和 app.config
例如,
将在相应的 cscfg 文件中查找 StorageConnectionString 设置,然后它将搜索 web.config,然后搜索 app.config。
You can use CloudConfigurationManager in Azure SDK 1.7 http://msdn.microsoft.com/en-us/LIBRARY/microsoft.windowsazure.cloudconfigurationmanager
This starts by looking in the ServiceConfiguration.cscfg e.g. ServiceConfiguration.Cloud.cscfg for config setting. If it isn't there it falls back to web.config and app.config
For example
Will look in the appropriate cscfgfile for StorageConnectionString setting, then it will search the web.config and then app.config.
我们有许多环境(开发结构内的本地开发、开发结构外的本地开发、测试、发布,有 2 个版本:发布/产品和发布/暂存以及 20 个项目,其中一些项目需要在配置设置中进行一些可变性。我们解决了这个问题通过创建一个小的“config”项目来解决这个问题,其中包含与环境匹配的子文件夹,在每次编译期间,我们根据正在执行的构建将文件从子文件夹复制到配置项目的根文件夹中
。 配置文件来避免在各种环境中始终重复相同的信息。
.config 文件的配置项目我们还使用部分
We have a number of environments (local dev inside dev fabric, local dev outside dev fabric, testing, release which has 2 versions: release/prod and release/staging and 20 projects some of which need some variability in configure settings. We solved this problem by creating a tiny "config" project, included subfolders there that match the environments. We copy files from the subfolder depending on which build we're doing into root folder of the config project, during every compile.
All other projects link to the config project for .config files. We also use partial config files to keep the insanity of repeating the same info all the time across various environments.
Hope this helps
我对转换 ServiceConfiguration 有相同的要求。
我接受了 jmac 的答案(谢谢!),但在基本版本中的命名空间方面遇到了麻烦:
经过一番探索后发现 此,作者:Andrew Patterson(谢谢)。
所以我得到的转换文件:
I had the same requirement for transforming ServiceConfiguration.
I went with the answer from jmac (thank you!), but had trouble with the namespace in the Base version:
after a bit more poking around found this by Andrew Patterson (Thank You).
so my resulting transform file: