Azure 连接字符串最佳实践

发布于 2024-10-03 08:49:10 字数 109 浏览 8 评论 0原文

我有一个刚刚迁移到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

窗影残 2024-10-10 08:49:10

如果开发人员是否可以看到生产凭据并不重要,您可以使用内置的 Visual Studio 10 配置转换。如果这是您正在寻找的内容,请按照下列步骤操作:

1.导航到文件资源管理器中的 Azure 项目文件夹
2. 复制 ServiceConfiguration.cscfg
3. 将副本重命名为 ServiceConfiguration.Base.cscfg
4. 对于每个构建配置(例如 Dev、Staging、Production),创建 ServiceConfiguration..cscfg 文件。在这些文件中,您可以使用普通的配置转换语法
5. 在文本编辑器中打开 .ccproj 文件
6. 找到以下节点,

<ItemGroup>
    <ServiceDefinition Include="ServiceDefinition.csdef" />
    <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
</ItemGroup>

并将其替换为以下节点(您必须编辑此块以匹配您的构建配置):

<ItemGroup>
    <ServiceDefinition Include="ServiceDefinition.csdef" />
    <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
    <None Include="ServiceConfiguration.Base.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
    <None Include="ServiceConfiguration.Dev.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
    <None Include="ServiceConfiguration.Staging.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
    <None Include="ServiceConfiguration.Production.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
</ItemGroup>

7. 在 .ccproj 文件末尾添加以下内容,就在 上方;:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
<Target Name="BeforeBuild">
    <TransformXml Source="ServiceConfiguration.Base.cscfg" Transform="ServiceConfiguration.$(Configuration).cscfg" Destination="ServiceConfiguration.cscfg" />
</Target>

8.如果您使用的 CI 服务器未安装 Visual Studio 10,则可能需要复制 C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\ Web 文件夹及其内容从开发计算机传输到服务器。

更新:正如@SolarSteve指出的,您可能必须将命名空间添加到 ServiceConfiguration.*.cscfg 文件中。以下是 ServiceConfiguration.Base.cscfg 的示例:

<sc:ServiceConfiguration serviceName="MyServiceName" osFamily="1" osVersion="*" xmlns:sc="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <sc:Role name="MyRoleName">
    <sc:Instances count="1" />
    <sc:ConfigurationSettings>
      <sc:Setting name="DataConnectionString" value="xxx" />
    </sc:ConfigurationSettings>
  </sc:Role>
</sc:ServiceConfiguration>

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,

<ItemGroup>
    <ServiceDefinition Include="ServiceDefinition.csdef" />
    <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
</ItemGroup>

and replace it with this (you will have to edit this block to match your build configs):

<ItemGroup>
    <ServiceDefinition Include="ServiceDefinition.csdef" />
    <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
    <None Include="ServiceConfiguration.Base.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
    <None Include="ServiceConfiguration.Dev.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
    <None Include="ServiceConfiguration.Staging.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
    <None Include="ServiceConfiguration.Production.cscfg">
        <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
    </None>
</ItemGroup>

7.Add the following at the end of the .ccproj file, just above </Project>:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
<Target Name="BeforeBuild">
    <TransformXml Source="ServiceConfiguration.Base.cscfg" Transform="ServiceConfiguration.$(Configuration).cscfg" Destination="ServiceConfiguration.cscfg" />
</Target>

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:

<sc:ServiceConfiguration serviceName="MyServiceName" osFamily="1" osVersion="*" xmlns:sc="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <sc:Role name="MyRoleName">
    <sc:Instances count="1" />
    <sc:ConfigurationSettings>
      <sc:Setting name="DataConnectionString" value="xxx" />
    </sc:ConfigurationSettings>
  </sc:Role>
</sc:ServiceConfiguration>
故事灯 2024-10-10 08:49:10

就我们个人而言:

  1. 完全放弃了 Web 配置转换。
  2. 从 cscfg 检索设置。
  3. cscfg 的开发版本指向本地开发环境(存储在版本控制中)。
  4. 在部署到生产时,我们为生产 SQL Azure 和存储提供安全凭据。

有关扫描应用程序设置和云环境以获取配置值的设置管理类的示例,您可以查看开源 Lokad.CQRS对于 Windows Azure 项目(请参阅 CloudSettingsProvider)

Personally we:

  1. Dropped web config transformations completely.
  2. Setting is retrieved from cscfg.
  3. Development version of cscfg points to local development environment (that's stored in version control).
  4. While deploying to production, we supply secure credentials for production SQL Azure and storage.

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)

笑看君怀她人 2024-10-10 08:49:10

您可以在 Azure SDK 1.7 中使用 CloudConfigurationManager http://msdn.microsoft.com/en-us/ LIBRARY/microsoft.windowsazure.cloudconfigurationmanager

首先在 ServiceConfiguration.cscfg(例如 ServiceConfiguration.Cloud.cscfg)中查找配置设置。如果不存在,则会回退到 web.config 和 app.config

例如,

CloudConfigurationManager.GetSetting("StorageConnectionString")

将在相应的 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

CloudConfigurationManager.GetSetting("StorageConnectionString")

Will look in the appropriate cscfgfile for StorageConnectionString setting, then it will search the web.config and then app.config.

稚气少女 2024-10-10 08:49:10

我们有许多环境(开发结构内的本地开发、开发结构外的本地开发、测试、发布,有 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

梅倚清风 2024-10-10 08:49:10

我对转换 ServiceConfiguration 有相同的要求。

我接受了 jmac 的答案(谢谢!),但在基本版本中的命名空间方面遇到了麻烦:

<ServiceConfiguration serviceName="TestCloud2" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="1" osVersion="*">

经过一番探索后发现 ,作者:Andrew Patterson(谢谢)。

所以我得到的转换文件:

<asc:ServiceConfiguration serviceName="TestCloud2" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:asc="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="1" osVersion="*">
<asc:Role name="WebRole1">
    <asc:Instances count="1" />
    <asc:ConfigurationSettings>
        <asc:Setting name="LoggingStorage" value="UseDevelopmentStorage=true" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </asc:ConfigurationSettings>
</asc:Role>

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:

<ServiceConfiguration serviceName="TestCloud2" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="1" osVersion="*">

after a bit more poking around found this by Andrew Patterson (Thank You).

so my resulting transform file:

<asc:ServiceConfiguration serviceName="TestCloud2" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:asc="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="1" osVersion="*">
<asc:Role name="WebRole1">
    <asc:Instances count="1" />
    <asc:ConfigurationSettings>
        <asc:Setting name="LoggingStorage" value="UseDevelopmentStorage=true" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </asc:ConfigurationSettings>
</asc:Role>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文