.NET Configuration (app.config/web.config/settings.settings)

后端 未结 13 1275
广开言路
广开言路 2020-11-29 14:41

I have a .NET application which has different configuration files for Debug and Release builds. E.g. the debug app.config file points to a development SQL Server which has d

13条回答
  •  隐瞒了意图╮
    2020-11-29 15:21

    Our project has the same issue where we had to maintain configs for dev, qa, uat and prod. Here is what we followed (only applies if you are familiar with MSBuild):

    Use MSBuild with the MSBuild Community tasks extension. It includes the 'XmlMassUpdate' task that can 'mass-update' entries in any XML file once you give it the correct node to start with.

    To Implement:

    1) You need to have one config file which will have your dev env entries; this is the config file in your solution.

    2) You need to have a 'Substitutions.xml' file, that contains only the entries that are DIFFERENT (appSettings and ConnectionStrings mostly) for each environment. Entries that do not change across the environment need not be put in this file. They can live in the web.config file of the solution and will not be touched by the task

    3) In your build file, just call the XML mass update task and provide the right environment as a parameter.

    See example below:

        
        
            
            
        
    
        
        
          
            
               
                
                           
            
            
              
                
                          
            
         
        
    
    
    
    
        
                
            
    

    replace '$Environment' with 'QA' or 'Prod' based on what env. you are building for. Note that you should work on a copy of a config file and not the actual config file itself to avoid any possible non-recoverable mistakes.

    Just run the build file and then move the updated config file to your deployment environment and you are done!

    For a better overview, read this:

    http://blogs.microsoft.co.il/blogs/dorony/archive/2008/01/18/easy-configuration-deployment-with-msbuild-and-the-xmlmassupdate-task.aspx

提交回复
热议问题