Windows Service Installer using WiX

I will create a simple windows installer. I do not pretend you should use it or the code bellow is a generic solution for building a windows service installers. The full source code is available here

Currently the most flexible free way for creating installers is WiX. The WiX installer adds several project templates to Visual Studio and also adds MSBuild targets for WiX support.

From Visual Studio create Windows Installer XML => Setup Project. I want to create a setup project for a windows service project and that windows service project contains dependencies. The main problem is how to handle project dependencies effectively. I could simply add all references by hand and write them in Product.wxs file but that is not a good solution for me. So I will directly use heat.exe which is part of the WiX tools to collect all files needed and the output result will be Product.wxs with all the dependencies. Heat will be called before building the setup/installer project on PreBuildEvent. This tool has a lot of parameters and I will not explain what they do here but there are some mandatory like target directory or output. The first important parameter is the directory which will be scanned by heat but the directory is also used for building the correct paths for all files referenced in Product.wxs. In this example all projects output the assemblies in a common bin folder. Defining that directory and passing it as a parameter is done directly within the project file (define SourceOutput under the common PropertyGroup; define a compiler constant SourceOutput=$(SourceOutput); pass the constant like “heat.exe” dir $(SourceOutput) -var var.SourceOutput). At this point all files under the bin folder will be added but heat has a way for transforming the results using *.xslt. The PreBuildEvent is now complete: <PreBuildEvent>”$(WIX)bin\heat.exe” dir $(SourceOutput) -cg References -srd -o $(ProjectDir)Product.wxs -nologo -gg -g1 -scom -sreg -t $(ProjectDir)Transformer.xslt -var var.SourceOutput</PreBuildEvent>

Essentially the Transformer.xslt is the place where all the changes must be done because the Product.wxs file will be overwritten every time the solution is built (-o $(ProjectDir)Product.wxs). After adding the Transformer.xslt to the project all the infrastructure is done and the project file looks like this:

WiX has the notion of Product, Feature, Component. I see it like this way. Product is the main place for configuring the installer package. It also holds one or more Features. Defining a Feature enables the option whether to install or not install a particular feature at setup time. Component describes the content of the Feature and this is the place which is important.

The first thing to do is to change the variable section <xsl:variable>.

In the bin folder there are other projects’ assemblies which should not be in the final installer package. I want only the windows service output directory NMSD.StatsD-PerfMon to be added. Easy. The template called Component1_ReferencesTemplate filters the directories but you have to manually write the name of the desired directory in the foreach statement: and (contains(@Source, ‘NMSD.StatsD-PerfMon’))

After heat has finished its job all the file tags in Product.wxs  will have a property called KeyPath which is wrong. Only the windows service entry point NMSD.StatsD-PerfMon.exe must contain that property. The xslt template at the bottom corrects this but you have to manually write the name of the file which will contain the KeyPath property.

In my case I did an installer with two features. Both features were developed heavily and I did several releases/deploys using the installer and I did not edit the xslt even once. I just do rebuild with psake and everything is working.

Posted in WiX

Visual Studio Extensions – Format document on Save

Since Visual Studio 2008 I am using this handy set of tools Power Commands. One of its features is essential for me: Format document on save. From their documentation it is clear what this does. The Format document on save option formats the tabs, spaces, and so on of the document being saved. It is equivalent to pointing to the Edit menu, clicking Advanced, and then clicking Format Document. I would also add the key shortcut CTRL+E,D

One week ago I switched to Visual Studio 2013 and the extension is not available yet for the new dev environment. Years ago I could do a Macro for this task but Macros are out from Visual Studio. Also addins are deprecated in the new VS.  So I decided to create a simple small extension which does exactly this. I have never create such an extension. But people are doing this so easily and there are so many extensions online. That should be easy. Well… I took me two days (4-5 hours total) for such a simple task.

Step 1: How should the extension behave?

When the developer presses CTRL+S the command Editor.FormatDocument should be invoked to format the current document.

Step 2: How to create a simple Visual Studio Extension?

I started with the docs – Extending Visual Studio Overview. Soon I found that the only way to hook before the actual document save, format the document and then return to the default chain of commands is possible only with the IOleCommandTarget interface. In the MSDN there are many examples for different kinds of extensions. There was one for my case: Walktrough: Using a Shortcut Key with an Editor Extension. I did exactly that tutorial to feel how the things are working. It was a total mess for me. In the end of this step at least I knew that I need Editor Viewport Adornment project for my little extension.

Step 3: Get the sample project working with CTRL+S?

There is no easy way. I just debugged it. Lines 16-17 test for the CTRL+S combination.

Step 4: How to invoke the command Edit.FormatDocument?

The easiest way to do this is to invoke a command trough the DTE object. The provider from the extensions resolves the DTE instance and passes it to the KeyBindingCommandFilter. The rest is piece of cake. Calling the command is in the code sample above. Resolving the DTE and passing it to the filter is below.

I also added several references. The final list of the references is:

  • envdte
  • Microsoft.VisualStudio.CoreUtility
  • Microsoft.VisualStudio.Editor
  • Microsoft.VisualStudio.OLE.Interop
  • Microsoft.VisualStudio.Shell.12.0
  • Microsoft.VisualStudio.Shell.Immutable.10.0
  • Microsoft.VisualStudio.Text.UI
  • Microsoft.VisualStudio.TextManager.Interop
  • System
  • System.ComponentModel.Composition
  • System.Core

Step 5: Submit to the extensions gallery

The final result is uploaded to the gallery here.

The full source code is also available in github here.

Tagged with: ,
Posted in Tools

How to change MSSQL Management Studio 2012 theme

Currently my version of ssms is Microsoft SQL Server Management Studio 11.0.3128.0

When you do the steps below and you try to open ssms you may get an error and nothing will happen. Just Open/Close ssms until you open ssms without error and then change/edit the theme. After that you can go and delete the VSTheme folder. I know this is not perfect but it does the trick. In my case I just wanted to change the silly “blinking” blue background.

  1. Download and open (not install) Visual Studio Color Theme Editor
  2. Copy/Paste the files to C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Extensions\VSTheme\
  3. Overwrite the content of extension.vsixmanifest with the following content:
Posted in MSSQL 2012

Configure Npgsql on Windows

First I downloaded the Postgres from here. (version  is 9.3.0 – win x32-64). So, installer like any other installer. In the end I choose to install Npgsql and pgAdmin. The first is the driver and the second is a nice GUI so you can see some data within the database. I expected that the installer for the Npgsql will integrate nicely to my machine and everything will start working. Yeah, it did not.

I will describe how to connect to Postgres database using a connection string and Npgsql provider. The main difficulty I had the first time was the providerName which I had to provide:

Well, I could provide OLEDB or ODBC provider but I wanted to use the native Npgsql provider. There are two key points to get things working. Depending on the machine environment the steps may be different from mine. So here we go:

  1. Register Npgsql.dll and Mono.Security.dll to the GAC (Global Assembly Cache)
  2. Add DbProviderFactory in the machine.config

For the first step I used gacutil.exe. I found that util in four different places on my machine. I tried the util from this location: “C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\gacutil.exe“. OK, the installer of Npgsql installed everything under this folder: “C:\Program Files (x86)\PostgreSQL\Npgsql” but next time I am doing this may be I will skip the installer and I would download a .zip with the binaries because the installer did not do much.  Another thing to mention here is the version of the Npgsql driver and gacutil.exe. What I mean is that gacutil.exe under under folder “C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin” will refuse to register Npgsql.dll for .Net4.0


For the second step just add a new entry in machine.config under <> tag. I added the DbProviderFactory for Npgsql on both 32-bit and 64-bit. Make sure version number and public key token are correct.

32-bit => C:\Windows\Microsoft.NET\Framework\[version]\config\machine.config

64-bit => C:\Windows\Microsoft.NET\Framework64\[version]\config\machine.config


And now the final connection string is:

Posted in postgres