在发布模式下,是否有自动使用单独的app.config的方法?
换句话说,我想用一个app.config进行测试,用另一个发布。
目前,我保留了一个名为app.config.production的副本,并在发布构建后手动覆盖bin\Release\Application.exe.config。
在发布模式下,是否有自动使用单独的app.config的方法?
换句话说,我想用一个app.config进行测试,用另一个发布。
目前,我保留了一个名为app.config.production的副本,并在发布构建后手动覆盖bin\Release\Application.exe.config。
<PropertyGroup>
<AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>
最近我在类似的SO话题上发布了一个超级迟到的回复: https://dev59.com/FHA75IYBdhLWcg3w0cnx#27546685
为了便于理解,我会在这里重复一遍:
我发现了一种实现app.config
文件的web.transform
方法的好方法。(即它利用了命名空间http://schemas.microsoft.com/XML-Document-Transform
)
我认为它很“好”是因为它是一种纯xml方法,不需要第三方软件。
一个父/App.config文件会根据不同的构建配置进行修改。 然后这些子配置只覆盖需要修改的地方。 在我看来,这比其他答案中需要维护x个完整的配置文件要更加复杂和健壮。
可以在这里找到一篇操作指南:http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/
看,妈妈——我的IDE没有显式的后期构建事件!
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
中的v10.0
替换为v15.0
。 - Jean-François Beauchampv10.0
替换为 v$(VisualStudioVersion)
,以使此解决方案与任何 VS 版本兼容。 - OronDF343一种简洁的解决方案是将两个文件App.Debug.config
和App.Release.config
组合成一个App.config
文件,并根据编译时的配置将有效的文件更改为App.config
:
<ItemGroup>
<None Include="App.config" />
<None Include="App.Debug.config">
<DependentUpon>App.config</DependentUpon>
</None>
<None Include="App.Release.config">
<DependentUpon>App.config</DependentUpon>
</None>
</ItemGroup>
<Target Name="SetAppConfig" BeforeTargets="Compile">
<Copy SourceFiles="App.Debug.config" DestinationFiles="App.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)' == 'Debug' " />
<Copy SourceFiles="App.Release.config" DestinationFiles="App.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)' == 'Release' " />
</Target>
通过这个解决方案,在Visual Studio中您将获得类似于以下截图的效果:
一种简单快捷的方式是创建一个名为 "App.release.config" 的第二个文件,并插入以下预构建事件:
IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.config" "$(ProjectDir)App.debug.config"
IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.release.config" "$(ProjectDir)App.config"
还有这个构建后事件:
IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.debug.config" "$(ProjectDir)App.config"
这可能有点奇怪,但它能让你将 .Settings
文件作为调试设置继续使用,并且仍然与 App.config
相关联。需要手动构建 App.release.config
,但很容易切换此功能。
我强烈推荐使用SlowCheetah进行app.config转换。在Visual Studio Gallery中可以找到这个NuGet宝石。
与顶部答案类似,但是使用这种方法,如果需要,您可以查看实际文件,Intellisense在csproj文件中不会出现错误:
<Target Name="SetAppConfig" BeforeTargets="Compile">
<Copy SourceFiles="debug.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " />
<Copy SourceFiles="release.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
</Target>
我不确定这是否有帮助,但是app.config会识别标准的MSBUILD替换字符串,例如$(Configuration)。