Managing configuration with multiple environments in Visual Studio - the right way!

Managing configuration with multiple environments in Visual Studio - the right way!

It's very common we develop a web application using App.config/Web.config. The config usually has different values for different environments - connection Strings, URL's etc... When publishing the web application to the server, it uploads the config file together. If you don't know how to handle the configuration in Visual Studio properly, you will find yourself overwriting the config file in every environment for every publish.

So let me show you the right way to do it:

Let's say we developed Web API Service in the development environment, And we have in the Web.config a connection String named 'MyDB', which points to the development environment:

In order to publish this site with a different value of the connection String, we will have to do the following:

1. Go to the Configuration Manager:

2. Choose '<New..>' in the 'Active solution configuration':

3. Add the name of the environment in your organization, for example, 'QA'. You can copy the existing config file to save time, instead of writing the config again


4. After you press 'OK', validate that your application has its correct config file name, and close the editor:


5. Go back to your Web.config, and notice when you open it that you will see 'Web.Debug.config' and 'Web.Release.config'. The 'Web.Debug.config' is for the development environment, and the 'Web.Release.config' is for the production environment. (I recommend that you read more about the release, because it is doing a lot of optimizations on the .dll files, which sometimes causes the site to act differently from the regular .dll's.) 



6. Now, to see the your new 'QA' config file, right click the 'Web.config', and choose 'Add Config Transform':





7. Look inside the QA file: basically, if you don't write anything; it will take the same config as the 'Web.config'. If you have this declaration in your file:

'<compilation  xdt:Transform="RemoveAttributes(debug)" />

You will not be able to debug this environment, so if you if you want to be able to perform debug - so, remove this declaration.

Let's say you want to change the value of your 'MyDB' connection Strings. Then copy the connection Strings, change the value, and add this declaration:

xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

at the end of your connectionString. This will tell the config to replace the connectionStrings with the same name as the current value. There are more options, such as 'Delete' etc... You are invited to read more about them here:

8. Create Publish Profiles: right click on your web application and click 'Publish':





9. For this demonstration, we will publish our site to a folder in the server. So, choose 'Folder' as the publish target, insert the location, and in the bottom choose 'Create Profile' and click


10, Now click 'Actions' -> 'Rename Profile', give it a normal name, and save:


11. Now, let's connect the 'Publish Profile' and the right config file. click 'Configure' -> 'Settings' -> choose the right config file:

If you will have problems with the publish of the configuration, try selecting 'Delete all existing files prior to publish', under 'File Publish Options'.


12. Now return 1-11 steps to add more configs and profiles as the number of the environments you have.


Last thing, you should pay attention that now you have for every config separate folder in the bin, so if you have problems with debugging etc... you should ensure that your running on the right environment:



Thanks,

Ziv Ben-Or

[email protected]

+972545882011

Craig Gers

Senior Software Engineer at Evolve Group

1 年

Which extension or Nuget package did you use, Slow Cheetah, Fast Koala or other?

回复
Dean Holbrook

Software Consultant

3 年

As a .NET novice, I found this article Very informative!

回复

要查看或添加评论,请登录

Ziv Ben-Or的更多文章

  • Different ways to return data in C#

    Different ways to return data in C#

    When we write a function with complex logic or when the function calls for a third party resource - aside from the…

    1 条评论
  • How To Handle Cases By Range in C# (and JavaScript)

    How To Handle Cases By Range in C# (and JavaScript)

    Sometimes you need to handle cases within a certain range, e.g.

  • 3 Questions for you to see how well do you know JavaScript

    3 Questions for you to see how well do you know JavaScript

    Here are 3 question to examine your JavaScript knowledge. Please try to answer before you run and execute the code.

    3 条评论
  • Microsoft Dynamics CRM 365 - 3 JavaScript Bugs

    Microsoft Dynamics CRM 365 - 3 JavaScript Bugs

    We are working with Dynamics 365 V 8.2, and in the last year we found 3 bugs when trying to insert data into a record…

    1 条评论
  • Debug Web Resources with Fiddler

    Debug Web Resources with Fiddler

    Today I want to share with you a much easier and faster way to debug your Web-Resources during your development - with…

    3 条评论
  • Recently viewed items Performance issue/bug.

    Recently viewed items Performance issue/bug.

    Today I want to share with you one of the Microsoft's Dynamics CRM issues that causes a serious performance issue…

    12 条评论
  • CRM Visual Studio Extensions part 3

    CRM Visual Studio Extensions part 3

    Today I want to introduce you to Marat Deykun's extension – Microsoft Dynamics CRM Web Resources Updater (yes, a long…

    3 条评论
  • CRM Visual Studio Extensions part 2

    CRM Visual Studio Extensions part 2

    Today I want to introduce you to another Mads Kristensen extension – Web Compiler. This article is for those who…

  • CRM Visual Studio Extensions part 1

    CRM Visual Studio Extensions part 1

    I want to share with you the next series of articles with recommended Visual Studio extensions for Microsoft Dynamics…

  • C# - Using the Result Concept.

    C# - Using the Result Concept.

    I want to recommend you a great concept I'm working with – Result. A colleague of my, Tal Aitelberg, introduced me this…

    2 条评论