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

后端 未结 14 2698
走了就别回头了
走了就别回头了 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:28

    Inspired by Oleg and others in this question, I took the solution https://stackoverflow.com/a/5109530/2286801 a step further to enable the following.

    • Works with ClickOnce
    • Works with Setup and Deployment projects in VS 2010
    • Works with VS2010, 2013, 2015 (didn't test 2012 although should work as well).
    • Works with Team Build. (You must install either A) Visual Studio or B) Microsoft.Web.Publishing.targets and Microsoft.Web.Publishing.Tasks.dll)

    This solution works by performing the app.config transformation before the app.config is referenced for the first time in the MSBuild process. It uses an external targets file for easier management across multiple projects.

    Instructions:

    Similar steps to the other solution. I've quoted what remains the same and included it for completeness and easier comparison.

    0. Add a new file to your project called AppConfigTransformation.targets

    
      
      
        
        10.0
      
    
      
    
      
        
          
          $(IntermediateOutputPath)$(TargetFileName).config
        
        
      
    
      
      
        
        
      
    
    
    

    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 enironment so you might want to experiment with that.

    2. Unload project and open .csproj file for editing

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

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

    Find the project file section that contains all App.config and App.*.config references and replace as follows. You'll notice we use None instead of Content.

    
      
      
        app.config
      
      
        app.config
      
      
        app.config
      
    
    

    4. Activate transformations magic

    In the end of file after

    
    

    and before final

    
    

    insert the following XML:

    
    

    Done!

提交回复
热议问题