Azure 连接字符串最佳实践 [英] Azure connection string best practices
问题描述
我有一个刚刚迁移到 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?
推荐答案
如果开发人员能否看到生产凭据并不重要,您可以使用内置的 Visual Studio 10 配置转换.如果这是您要查找的内容,请按照以下步骤操作:
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.在文件资源管理器中导航到您的 Azure 项目文件夹
2.复制ServiceConfiguration.cscfg
3. 将副本重命名为 ServiceConfiguration.Base.cscfg
4. 对于每个构建配置(例如 Dev、Staging、Production),创建一个 ServiceConfiguration.
5. 在文本编辑器中打开 .ccproj 文件
6. 找到以下节点,
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.在.ccproj文件的末尾添加以下内容,就在的上方:
7.Add the following at the end of the .ccproj file, just above </Project>
:
<Import Project="$(MSBuildExtensionsPath)MicrosoftVisualStudiov10.0WebMicrosoft.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 FilesMSBuildMicrosoftVisualStudiov10.0Web 文件夹和其内容从开发机器到服务器.
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 FilesMSBuildMicrosoftVisualStudiov10.0Web folder and its contents from a development machine to the server.
更新:正如@SolarSteve 指出,您可能需要向 ServiceConfiguration.*.cscfg 文件添加命名空间.以下是 ServiceConfiguration.Base.cscfg 的示例:
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>
这篇关于Azure 连接字符串最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!