VS 2008

How to handle builds & deployments for multiple environments in .NET

I came across a situation a while back, where I needed to setup a build process for a Visual Studio 2008 solution that contained multiple projects:

  • A bunch of Console Applications,
  • A mobile app (targeted at an enterprise-class rugged Windows Mobile 6.5 device to be used on field),
  • A desktop Windows Forms application &
  • An ASP.NET web application.

We have 3 server environments: Dev, QA & Production on which each of these applications should be capable of running. (Except for the mobile app, which will run on its own SQLCE database file sitting on each hand-held unit). What I needed to come up with, was a way to manage the environment-specific configuration info in a manner that let me do a single build, and “apply” the right set of configuration data according to where I was doing the deployment.

The direct way to do this would be to use the VS Solution/Project Configuration functions. You would create one configuration for each environment. Then you would set up a pre-build event like <code> copy “$(ProjectDir)$(ConfigurationName).<<configfilename>>” “$(ProjectDir)$(OutDir)<<configfilename>>” /Y </code> to copy the right config file over to your build output directory. You can check out this excellent (albeit really old, considering this is VS 2008!) article from Bil Simser –
http://weblogs.asp.net/bsimser/handling-multiple-environment-configurations-with-net-2-0
or this one from Scott Hanselman –
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

The disadvantage here is that you need to do a build with the appropriate configuration selected, in order to get the right set of files selected. I would prefer to have a single build output file, like a TAR/ZIP archive, which includes all the config files for all environments. Then when I deploy/unpack it to the right environment, I want to deploy the right config files based on an argument that I’ll pass into my deployment script. Personally, that makes it easier to control for me, rather than pick the right build configuration on the VS IDE every time. It also enables me to do everything on the command-line too if I want, which is good to have.

Being a .NET newbie, I didn’t have the time to figure out Nant (which is free & open-source btw), so I used Visual Studio’s pre-build & post-build events to make things work the way I needed them to. Here is how it went.

(more…)

How to use LINQ to work with XML (Visual Studio 2008)

Note: I still use VS 2008 for software development, as do several developers out there working for large corporate entities that are much slower to upgrade technologies. I’ve only really tested LINQ with VS 2008, although it is possible it might work the same way with newer versions.

LINQ, or Language-Integrated Query is a .NET Framework component that adds native querying capability to .NET languages. LINQ with XML lets you work with XML documents very easily. I like to use the LINQ to XSD Preview for my XML work in VS 2008. The LINQ to XSD Preview is basically a project that provides typed XML programming support on top of the existing LINQ to XML framework. With this, you can generate classes for your XML objects defined in a XSD.

(more…)