【发布时间】:2011-01-23 21:07:00
【问题描述】:
在 .NET / SQLServer 应用程序中管理调试和发布连接字符串的好方法是什么?
我有两个 SQL Server,一个生产和一个构建/调试,当我的 ASP.NET 应用程序部署时,我需要一种在两者之间切换的方法。
目前我只是将它们存储在 web.config 中并注释掉其中一个,但是在部署时很容易出错。
【问题讨论】:
标签: c# asp.net vb.net connection-string
在 .NET / SQLServer 应用程序中管理调试和发布连接字符串的好方法是什么?
我有两个 SQL Server,一个生产和一个构建/调试,当我的 ASP.NET 应用程序部署时,我需要一种在两者之间切换的方法。
目前我只是将它们存储在 web.config 中并注释掉其中一个,但是在部署时很容易出错。
【问题讨论】:
标签: c# asp.net vb.net connection-string
当您打开一个 Web 项目时,您将获得 2 个额外的 Web.Config 文件 - Web.Debug.config 和 Web.Release.config。
1.将所需的连接字符串添加到具有 XSLT 属性的文件 xdt:Transform="SetAttributes" xdt:Locator="Match(name)"
<connectionStrings>
<add name="myConnectionString" connectionString="myConnectionString" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>"
2.编辑您的 csproj 并添加一个 TransformXml 目标:
<Target Name="TransformActiveConfiguration" Condition="Exists('$(ProjectDir)/Web.$(Configuration).config')" BeforeTargets="Compile" >
<TransformXml Source="$(ProjectDir)/Web.Config" Transform="$(ProjectDir)/Web.$(Configuration).config" Destination="$(TargetDir)/Web.config" />
</Target>
第二步将在每次构建时进行转换(根据您的活动配置),而不仅仅是在发布时,为您提供更好的调试体验。我是从this post那里学到的。
【讨论】:
自 2018 年起,对于较新版本的 Visual Studio,Microsoft 已接管了 SlowCheetah 扩展。安装它后,您可以选择将 app.config 文件拆分为三个单独的文件。一个是包含始终适用的代码的基本文件,然后您将获得一个 app.debug.config 文件和一个 app.release.config 文件。
请注意,在项目中将其作为 NuGet 包拉取是不够的。如果您想要 Visual Studio UI 菜单选项,您需要从以下站点实际下载安装程序并运行它。然后,将 SlowCheetah 专门安装在您想要使用 NuGet 的任何项目上。
另请注意,原始开发人员的原始 SlowCheetah 程序仍然存在,但请使用 Microsoft 为较新版本的 Visual Studio 发布的程序。
【讨论】:
我可以说这个问题的其他解决方案。在 csproj 文件文件中创建如下:
<Content Include="DB.config">
<SubType>Designer</SubType>
</Content>
<Content Include="DB.Debug.config">
<DependentUpon>DB.config</DependentUpon>
<SubType>Designer</SubType>
</Content>
<Content Include="DB.Release.config">
<DependentUpon>DB.config</DependentUpon>
<SubType>Designer</SubType>
</Content>
在 xml 中设置了发布和调试的两个版本。
【讨论】:
使用预处理器指令:当您的项目配置为在调试模式下运行时,将选择调试连接字符串,否则将自动选择发布连接字符串。
在 Visual Studio 中,您会注意到语句仅根据项目配置(调试或发布)变暗。
只需在您的代码中添加类似以下内容:
string myConnectionString;
#if DEBUG
myConnectionString = "your debug connection string";//may be read from your debug connection string from the config file
#else
myConnectionString = "your release connection string"; //may be read from your relase connection string from the config file
#endif
更多详情,请查看this。
【讨论】:
我在 .net 3.5 中结合使用 Sameh 和 Obalix 的方法。
public static class DataConnection
{
#if LOCALDEV
public const string Env = "Debug";
#endif
#if STAGING
public const string Env="Staging";
#endif
#if RELEASE
public const string Env="Release";
#endif
private static ConnectionStringSettingsCollection _connections;
static DataConnection()
{
_connections = ConfigurationManager.ConnectionStrings;
}
public static string BoloConnectionString
{
get
{
return _connections["DB1."+Env].ConnectionString;
}
}
public static string AOAConnectionString
{
get
{
return _connections["DB2."+Env].ConnectionString;
}
}
public static string DocVueConnectionString
{
get
{
return _connections["DB3."+Env].ConnectionString;
}
}
}
然后在我的项目属性中,我定义了正确的条件编译符号。这样,我就不必像 Sameh 那样对连接字符串进行硬编码,但代码仅根据其构建方式查找字符串。这让我拥有(如果需要)所有构建的一个配置文件,但实际上我不会在构建过程中部署配置文件。尽管 .net 4 的条件 app.Relase.config 看起来是未来的正确方法。
【讨论】:
创建 Web.config 文件的调试和发布版本,例如Web.debug.config 和 Web.release.config。然后添加一个预编译条件,根据当前 Target 将相关版本复制到 web.config 中。
编辑:要添加预编译条件,请右键单击您的项目并选择“属性”,然后转到“构建事件”选项卡并将以下代码添加到预编译条件中。显然,您必须根据需要修改代码,请参见下图。
@echo off
echo Configuring web.config pre-build event ...
if exist "$(ProjectDir)web.config" del /F / Q "$(ProjectDir)web.config"
if "$(ConfigurationName)" == "Debug Test" goto test
if "$(ConfigurationName)" == "Debug M" goto M
if "$(ConfigurationName)" == "Debug BA" goto BA
if "$(ConfigurationName)" == "Release Test" goto test
if "$(ConfigurationName)" == "Release M" goto M
if "$(ConfigurationName)" == "Release BA" goto BA
echo No web.config found for configuration $(ConfigurationName). Abort batch.
exit -1
goto :end
:test
copy /Y "$(ProjectDir)web.config.test" "$(ProjectDir)web.config"
GOTO end
:BA
copy /Y "$(ProjectDir)web.config.BA" "$(ProjectDir)web.config"
GOTO end
:M
copy /Y "$(ProjectDir)web.config.M" "$(ProjectDir)web.config"
GOTO end
:end
echo Pre-build event finished
Project Properties http://img442.imageshack.us/img442/1843/propsa.jpg
【讨论】:
好消息是 .NET4 有 a provision,为此,您可以为每个配置(web.Release.config、web.Debug.config)设置单独的配置。
坏消息是……你可能还没有使用它。
【讨论】:
也许这有点过时了,但ODBC DSN 很好地解决了这个问题——我仍然使用——严格——DNS 设置来区分生产环境和调试环境。
p.s.,我预计会有大量的反对票,也许这将衡量人们对数据库标识符间接级别的看法。
【讨论】:
我通常在我的生产服务器上设置一个环境变量,表示该服务器是生产服务器。然后,我根据此环境变量是否存在并设置为生产值,从我的 web.config 中读取正确的连接字符串。
【讨论】: