Galin Iliev's blog

Software Architecture & Development

.NET External Configuration & Build Process

It is very good practice to have several environments when creating a software solution - typical environments are Development -> Integration -> Staging -> Production environments. Also having Automated Continuous Integration server like CruiseControl.NET can greatly improve teamwork and quality of developed solution. Of course having a solution build (by CruiseControl.NET) you might want to have the installation project also build... and you will need a deployment procedure so the steps would look like these:

  1. Check-in source code changes
  2. Trigger a build on CI build server for the solution
  3. Trigger a build on CI build server for the build package
  4. Execute publish script which will deploy binaries to certain environment/servers and will change app settings, connection strings etc.

Example project setup can be seen on Omar Al Zabir's blog post ASP.NET website Continuous Integration+Deployment using CruiseControl.NET, Subversion, MSBuild and Robocopy.

 

Of course having all changes for different environments in publish script would make it big and difficult to maintain. This is why it is better to keep all environment/servers specific settings outside of the project. One option is machine.config. And storing it in source control, of course.

 

Another option is having all those settings in separate folder. I wasn’t aware of this option of .NET Configuration API and I was disappointed  when I found that XInclude is not supported. Fortunately there configSource section attribute which allows to achieve same functionality. For some ( more here ) this might be well known but I found this recently and AFAIK it is not widely used.

 

So you can specify web.config/app.config file like this:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:   <connectionStrings configSource="ConnectionStrings.config" ></connectionStrings>
   4:   <appSettings configSource="settings.config"></appSettings>
   5: </configuration>

and then specify actual configuration in external files. Here are my examples for ConnectionStrings.config

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <connectionStrings>
   3:   <add name="cs1" connectionString="Data Source=myServerAddress;Failover Partner=myMirrorServer;Initial Catalog=myDataBase;Integrated Security=True;"/>
   4: </connectionStrings>

and Settings.config

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <appSettings>
   3:   <add key="s1" value="Some very important setting"/>
   4: </appSettings>

 

As you can see from example the only important thing is to have root element of external file named same as referenced section in the core config file.

and after that you can simply get the values as usual:

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         Console.WriteLine("cs1: {0}", ConfigurationManager.ConnectionStrings["cs1"].ConnectionString);
   6:         Console.WriteLine("s1: {0}", ConfigurationManager.AppSettings["s1"]);
   7:         Console.ReadLine();
   8:     }
   9: }

 

This means you can refactor configuration of existing .NET applications without having to touch the code or even recompile. Just be careful :)

Happy XML/Config refactoring :) !

MVP or Blue badge

Which one is better?! No doubt both are very excellent in the world of software development with Microsoft tools. So I feel very lucky because last month I touched both in one or another way:

  • In the beginning of September I got a request for more detailed contact information regarding a upcoming nomination for MVP Award. Although very pleased I couldn't accept such nomination as I already had accepted an offer from Microsoft for fulltime employment.
  • In the beginning of October 2008 I've started working for Microsoft as fulltime employee(or as a blue badge in local slang :) ). I am working as SDE in AdCenter and I will be creating solutions with favorite .NET Framework. No need to say that I am very excited and impatient to get into deep details in organization processes inside the biggest and most successful software company.
    I also relocated from Bulgaria to Seattle with my wife and we had the longest journey so far. The international relocation package is excellent and several teams work with us to facilitate the relocation and to make it as painless as possible. The friends here helped us a lot for getting acknowledged with the area and the lifestyle.
What does this mean for the blog?!

Well... I will keep it for sure!!! Microsoft supports such initiatives but definitely you won't read news here prior officially announced :) so Scott Guthrie will remain your preferred source of fresh news regarding development tools. There were some suggestions to move the blog on blogs.msdn.com but I think I will keep it on http://www.galcho.com/blog/.

 

The photo album is also updated with various photos from our area exploration tours in Seattle/Redmond/Bellevue .