Friday, April 29

Per Developer App.Config

We are working on C# project which use app.config to configure the way our application works.
Since we are using shared source control system we notice we each step on each others those when it comes to changes of the app.config. Each developer change things like the address of some service, which modules to load, etc.
One possible way is to make sure you are not checking-in your modified app.config, but that tedious.

We wanted to have the normal app.config with some reasonable default settings and have per developer app.config file like app_ido.config which will be used if present.

After reading Microsoft.Common.targets for a while I've found target called PrepareForBuild. I've notice that Microsoft were kind enough to use property called AppConfig if you define it. If you do not define it they try to find file called app.config which has build action of either None or Content (in that order).

So, I've create a new Console application, add to it app.config file and put some appSettings in it just to be able to tell which app.config I am using. I've then open the csproj file for edit and add new element inside the first PropertyGroup element called AppConfig and set it to 123. When I've compile it I've got an error that Application Configuration file "123" is invalid and could not be found. That was the way, the rest was easy.

I've change that property element to this:
<AppConfig Condition="Exists('App_$(USERNAME).config')">App_$(USERNAME).configAppConfig>

This did exactly what we needed. We chose to use the current Windows username as our variable to identify each developer. If file with the name of the developer is found - we use it, otherwise we fallback to normal app.config.

There was only one question open which is should we keep the developers app.config files in the source control system or not?

After some thinking we decide not to check them into the source control because those files are dispensable and can only cause harm like merge conflicts.

I hope this will help other developers looking for this behaviour.

You can download sample application, just change the file called app_Ido.config from Ido to your name and your good to go. Remember to rebuild the project after you the file name to see the effect.

1 comment: