【问题标题】:Visual Studio 2012: Switch from debug to realease easilyVisual Studio 2012:轻松从调试切换到发布
【发布时间】:2013-09-12 06:16:45
【问题描述】:

我们正在使用大约 5 个通过网络调用连接在一起的项目。

现在,Web 调用的 Uris 通过标签保存在 web.config 文件中。

<add key="ProductsAPIURL" value="http://192.168.1.4:5000" />

当我们处于调试模式时,我们会在所有项目中更改 api 调用的 uri 以匹配 localhost 端口。

在将其投入生产时,我们将其改回。

有没有其他方法可以轻松地从调试切换到发布,而无需每次都手动更改设置? (类似于创建一个可以记住所有这些设置的 Visual Studio 配置文件)

【问题讨论】:

  • 在代码中使用两个设置和#if DEBUG
  • 同意,但我需要有调试模式(编码时)、测试模式(内部测试时)和发布模式(发布时)
  • 这是一个新问题:如何将设置部署到生产环境

标签: c# visual-studio-2010 visual-studio visual-studio-2012


【解决方案1】:

您可以使用调试和发布配置。您将拥有以下文件:

Web.config
Web.Debug.Config
Web.Release.Config

您可以在Web.config 中定义一个连接字符串并在Web.Release.Config 中覆盖它:

Web.config:

<connectionStrings>
    <add name="MyConString" connectionString="Data Source=." />
</connectionStrings>

Web.Release.config:

<connectionStrings>
  <add name="MyConString" connectionString="Data Source=different" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

或者你可以使用预处理指令#if

#if DEBUG
    myConString = "Data Source=."
#else
    myConString = "Data Source=different"
#endif

【讨论】:

  • Visual Studio 中是否有约定知道在调试时需要使用Web.Debug.Config,在发布模式下编译项目时需要使用Web.Release.Config
  • Debug 上构建解决方案将导致它使用Web.Debug.config,在Release 中构建解决方案将导致它使用Web.Release.config。 (您可以在 Visual Studio 或构建配置中进行配置)
【解决方案2】:

配置转换将为您执行此操作,允许您拥有任意数量的配置,您可以在几秒钟内切换

http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx

【讨论】:

  • 这是一个很好的解决“额外部署问题”的方法。
猜你喜欢
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
相关资源
最近更新 更多