App.Config Transformation for projects which are not Web Projects in Visual Studio?

后端 未结 14 2528
走了就别回头了
走了就别回头了 2020-11-22 04:06

For Visual Studio 2010 Web based application we have Config Transformation features by which we can maintain multiple configuration files for different environments. But the

相关标签:
14条回答
  • 2020-11-22 04:13

    proposed solution will not work when a class library with config file is referenced from another project (in my case it was Azure worker project library). It will not copy correct transformed file from obj folder into bin\##configuration-name## folder. To make it work with minimal changes, you need to change AfterCompile target to BeforeCompile:

    <Target Name="BeforeCompile" Condition="exists('app.$(Configuration).config')">
    
    0 讨论(0)
  • 2020-11-22 04:18

    I tried several solutions and here is the simplest I personally found.
    Dan pointed out in the comments that the original post belongs to Oleg Sych—thanks, Oleg!

    Here are the instructions:

    1. Add an XML file for each configuration to the project.

    Typically you will have Debug and Release configurations so name your files App.Debug.config and App.Release.config. In my project, I created a configuration for each kind of environment, so you might want to experiment with that.

    2. Unload project and open .csproj file for editing

    Visual Studio allows you to edit .csproj files right in the editor—you just need to unload the project first. Then right-click on it and select Edit <ProjectName>.csproj.

    3. Bind App.*.config files to main App.config

    Find the project file section that contains all App.config and App.*.config references. You'll notice their build actions are set to None:

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

    First, set build action for all of them to Content.
    Next, make all configuration-specific files dependant on the main App.config so Visual Studio groups them like it does designer and code-behind files.

    Replace XML above with the one below:

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

    4. Activate transformations magic (only necessary for Visual Studio versions pre VS2017)

    In the end of file after

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    

    and before final

    </Project>
    

    insert the following XML:

      <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
      <Target Name="CoreCompile" Condition="exists('app.$(Configuration).config')">
        <!-- Generate transformed app config in the intermediate directory -->
        <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
        <!-- Force build process to use the transformed configuration file from now on. -->
        <ItemGroup>
          <AppConfigWithTargetPath Remove="app.config" />
          <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
            <TargetPath>$(TargetFileName).config</TargetPath>
          </AppConfigWithTargetPath>
        </ItemGroup>
      </Target>
    

    Now you can reload the project, build it and enjoy App.config transformations!

    FYI

    Make sure that your App.*.config files have the right setup like this:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
         <!--magic transformations here-->
    </configuration>
    
    0 讨论(0)
  • 2020-11-22 04:18

    So I ended up taking a slightly different approach. I followed Dan's steps through step 3, but added another file: App.Base.Config. This file contains the configuration settings you want in every generated App.Config. Then I use BeforeBuild (with Yuri's addition to TransformXml) to transform the current configuration with the Base config into the App.config. The build process then uses the transformed App.config as normal. However, one annoyance is you kind of want to exclude the ever-changing App.config from source control afterwards, but the other config files are now dependent upon it.

      <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
      <Target Name="BeforeBuild" Condition="exists('app.$(Configuration).config')">
        <TransformXml Source="App.Base.config" Transform="App.$(Configuration).config" Destination="App.config" />
      </Target>
    
    0 讨论(0)
  • 2020-11-22 04:18

    I have created another alternative to the one posted by Vishal Joshi where the requirement to change the build action to Content is removed and also implemented basic support for ClickOnce deployment. I say basic, because I didn't test it thoroughly but it should work in the typical ClickOnce deployment scenario.

    The solution consists of a single MSBuild project that once imported to an existent windows application project (*.csproj) extends the build process to contemplate app.config transformation.

    You can read a more detailed explanation at Visual Studio App.config XML Transformation and the MSBuild project file can be downloaded from GitHub.

    0 讨论(0)
  • 2020-11-22 04:18

    If you use a TFS online(Cloud version) and you want to transform the App.Config in a project, you can do the following without installing any extra tools. From VS => Unload the project => Edit project file => Go to the bottom of the file and add the following:

    <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
    <Target Name="AfterBuild" Condition="Exists('App.$(Configuration).config')">
    <TransformXml Source="App.config" Transform="App.$(Configuration).config" Destination="$(OutDir)\$(AssemblyName).dll.config" />
    

    AssemblyFile and Destination works for local use and TFS online(Cloud) server.

    0 讨论(0)
  • 2020-11-22 04:20

    Note: Due to reputation I cannot comment on bdeem's post. I'm posting my findings as an answer instead.

    Following bdeem's post, I did the following (in order):

    1. I modified the [project].csproj file. Added the <Content Include="" /> tags to the ItemGroup for the different config files and made them dependent on the original config file.

    Note: Using <None Include="" /> will not work with the transformation.

    <!-- App.config Settings -->
    <!-- Create App.($Configuration).config files here. -->
    <Content Include="App.config" />
    <Content Include="App.Debug.config">
      <DependentUpon>App.config</DependentUpon>
    </Content>
    <Content Include="App.Release.config">
      <DependentUpon>App.config</DependentUpon>
    </Content>
    

    2. At the bottom of the [project].csproj file (before the closing </Project> tag), I imported the ${MSBuildToolsPath\Microsoft.CSharp.targets file, added the UsingTask to transform the XML and added the Target to copy the transformed App.config file to the output location.

    Note: The Target will also overwrite the App.Config in the local directory to see immediate changes working locally. The Target also uses the Name="Afterbuild" property to ensure the config files can be transformed after the executables are generated. For reasons I do not understand, when using WCF endpoints, if I use Name="CoreCompile", I will get warnings about the service attributes. Name="Afterbuild" resolved this.

      <!-- Task to transform the App.config using the App.($Configuration).config file. -->
      <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
    
      <!-- Only compile the App.config if the App.($Configuration).config file exists. -->
      <!-- Make sure to use the AfterBuild name instead of CoreCompile to avoid first time build errors and WCF endpoint errors. -->
      <Target Name="AfterBuild" Condition="exists('App.$(Configuration).config')">
        <!-- Generate transformed App.config in the intermediate output directory -->    
        <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" />
        
        <!-- Modify the original App.config file with the transformed version. -->
        <TransformXml Source="App.config" Destination="App.config" Transform="App.$(Configuration).config" />
    
        <!-- Force build process to use the transformed configuration file from now on. -->
        <ItemGroup>
          <AppConfigWithTargetPath Remove="App.config" />
          <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
            <TargetPath>$(TargetFileName).config</TargetPath>
          </AppConfigWithTargetPath>
        </ItemGroup>
      </Target>
    </Project>
    

    3. Went back into Visual Studio and reloaded the modified files.

    4. Manually added the App.*.config files to the project. This allowed them to group under the original App.config file.

    Note: Make sure the App.*.config files have the proper XML structure.

    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
    
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <connectionStrings>
        <add name="myConn" connectionString=""; Initial Catalog=; User ID=; Password=;" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
      </connectionStrings>
    </configuration>
    

    5. Re-built the project.

    0 讨论(0)
提交回复
热议问题