更换 与AfterBuild .exe.config不适用 .vshost.exe.config

问题描述 投票:1回答:2

我们使用的AfterBuild目标在vbproj取决于所选择的配置,以替换配置文件:

<Target Name="AfterBuild" Condition="'$(Configuration)' != 'Release'">
  <Delete Files="$(TargetDir)$(TargetFileName).config" />
  <Copy SourceFiles="$(ProjectDir)$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" />
</Target>

举个例子,假设我们有3种配置:调试,测试,发布。调试是本地调试配置。测试是用于用户验收测试的一个预生产环境。发布是我们的生产环境。

App.config文件,我们保存我们的发行环境的配置。在Debug.config文件,我们保存我们的本地调试需求的配置。在Test.config文件中,我们存储了用户受理环境的配置。

所述AfterBuild目标的目标是建立/使用调试配置(App.config)或测试配置(Debug.config)上执行时,以取代Release配置(Test.config)。

一切都会按计划,当我们发布应用程序(版本,App.config),或者如果我们所建立的应用程序并启动bin\<appname>.exe(调试或测试)。

但是,如果我们开始从Visual Studio应用程序,与Visual Studio宿主进程,正确的配置复制到bin\<appname>.exe.config,但似乎Visual Studio中不复制的正确配置bin\<appname>.vshost.exe.config。我们试图清洗液,进行调试前的重建,发动前手动删除文件bin\<appname>.vshost.exe.config,但似乎托管过程总是副本从默认App.config文件中的配置。同样的问题发生,我们是否尝试与调试配置或测试配置开始。

为了增加混乱,我们创建了多个测试项目,使用相同的AfterBuild目标,和他们中的一些正常工作,有的则没有。所有项目都使用.NET Framework 4.5.1,但我们也有.NET 4.5再现的问题。它似乎并不受项目类型造成的,因为我们能够重现该问题与控制台应用程序以及Windows窗体应用程序。

这可能是造成这个问题?

另外,我们可以用另一种解决方案来管理我们的每一个环境的配置?

笔记

  • 我们使用默认的App.config文件为我们的发行环境,因为ClickOnce的似乎并不支持AfterBuild目标。
  • 我们使用Visual Studio Express的2013年Windows桌面,所以我们不能像使用任何SlowCheetah插件。
c# .net vb.net visual-studio app-config
2个回答
3
投票

继史蒂夫的建议,我们提出的逻辑来BeforeCompile目标,指定根据所选择的配置,以取代App.config

<Target Name="BeforeCompile">
    <Delete Files="$(ProjectDir)App.config" />
    <Copy SourceFiles="$(ProjectDir)$(Configuration).config" DestinationFiles="$(ProjectDir)App.config" />
      <Output TaskParameter="DestinationFiles" ItemName="FileWrites"/>
    </Copy>
</Target>

它不会每次都替换该文件,当它没有,它并不一定适用于<appname>.vshost.exe.config文件。我们无意中发现了另一个答案,指导我们在正确的道路上:Can Visual Studio automatically adjust the name of other file as it does with app.config?

添加以下<Copy>命令,我们达到了预期的行为:

<!--
Copy the application's .config file, if any.
Not using SkipUnchangedFiles="true" because the application may want to change
the app.config and not have an incremental build replace it.
-->
<Copy
    SourceFiles="@(AppConfigWithTargetPath)"
    DestinationFiles="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')"
    OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
    Retries="$(CopyRetryCount)"
    RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
    UseHardlinksIfPossible="$(CreateHardLinksForAdditionalFilesIfPossible)"
    >

    <Output TaskParameter="DestinationFiles" ItemName="FileWrites"/>

</Copy>

我们现在有每个配置(Debug.configTest.configRelease.config)一个配置文件。 Visual Studio中替换每个构建/启动一个正确的App.config文件。得到的<appname>.vshost.exe.config文件包含正确的参数。

奖金

这种解决方案的潜在好处是,我们每个配置中的一个配置文件,这样我们就可以更新.vbproj文件和替换,例如,

<None Include="Debug.config" />
<None Include="Release.config" />

<None Include="Debug.config">
    <DependentUpon>App.config</DependentUpon>
</None>
<None Include="Release.config">
    <DependentUpon>App.config</DependentUpon>
</None>

所有配置文件将在Visual Studio中的主要App.config下的文件进行分组:


0
投票

对于我来说,帮助禁用的VisualStudio托管进程。

删除复选标记:

Project -> Preferences -> Debug -> Enable Visual Studio hosting process

这将停止Visual Studio中从覆盖*配置文件。

© www.soinside.com 2019 - 2024. All rights reserved.