T4 Templates Update for Visual Studio 2015

A while back I posted a series of articles on how to use T4 and a custom VS extension to simplify some common code like application settings, WCF clients and environmental transforms. With the release of Visual Studio 2015 I had to update my own extension and templates so I wanted to posted a follow up article on the changes that need to be made to allow the extension to work with Visual Studio 2015. As part of the update I added some functionality to the app settings template. Before continuing be sure to download the previous version of the series (or simply download the final version below).

Creating Extensions in Visual Studio 2015

Fortunately VS2015 has not really changed extensions too much. The biggest change is that the required SDK and templates ship as part of Visual Studio properly or can be downloaded as an extension. If you did not install the extensibility tools as part of VS then install them now. Additionally, at least for the template sample in this article you will also need to install the T4 Toolbox extension.

For updating the VS2013 extension, open it in VS2015. If you get errors about projects that cannot be loaded then you have not installed the necessary SDK files yet.

Updating the Solution

Since very little has changed for extensions in VS2015 most changes revolve around updating the references to the newer version. The following references should be updated.

All references should be updated to point to the VS2015 version of the SDK or the Microsoft Shared folder. These assemblies are accessible via Reference Manager –> AssembliesExtensions. Remember that envDTE, envDTE80 and VSLangProj need to be modified (WindowsProperties) to have Embed Interop Types set to false. Note also that the projects should be targeting at least 4.5.2. Build the solution to ensure everything compiles correctly.

Modifying the Solution

In previous versions of VS a .pkgdef file could have a registry path and the extension would be correctly installed.

In VS2015 the registry path will not be created unless it is explicitly defined in the .pkgdef file. Therefore you have to add additional (empty) entries for all the paths from the base $RootKey$ to the target path.

Another change that needs to be made is that the extension is currently only for VS2013. To update to VS2015 we need to modify the Install Targets information in the .vsixmanifest file. Also at this time I am going to modify the extension to be per-user rather than per-machine.

On the Install Targets tab set the version range to [14.0] and set the product to be Microsoft.VisualStudio.Community

Uncheck the box that says This VSIX is installed for all users

Since the extension relies on T4 Toolbox, we need to update the Dependencies tab as well.

Go to the Dependencies tab

Remove the existing dependency on T4 Toolbox

Add the new dependency on the (already installed) extension

Because I want to support the older extension in VS2013 and the newer extension in VS2015 I also want to change the package ID so that it does not conflict with the other extension in the extension gallery.

Edit the Product ID to make it unique

Change the Product Name to identify it as for VS 2015

Changes to AppSettings Template

At this point the extension should now run in VS2015 but I wanted to make a few enhancements to the existing templates. I will not go into the details here so you can refer to the associated code but here are the core changes.

Switched the core template over to a fluent API for configuration to make it easier to read

Added option to create and/or implement an interface so that code can use an interface rather than the class

Removed ability to exclude a specific setting using ExcludeSetting – this is now handled through the fluent API

Removed OverrideSettingType – this is now handled through the fluent API

Added ConfigureSetting to allow the configuration of each setting including type, backing property and exclusion

Debugging Extensions in Visual Studio 2015

Debugging extensions in previous versions of VS hasn’t really changed but I have started relying on the experimental hive in VS2015 rather than using the normal hive. To do that I created a new shortcut to VS on my desktop and then modified the command line options to include /RootSuffix Exp. The experimental hive is basically a new configuration of VS that doesn’t include per-user extensions. If something goes wrong with this configuration then you still have the normal hive to work against. Since my extension requires T4 Toolbox I went ahead and installed it in the hive. Now whenever I need to debug my extension I can use the experimental hive and not worry about breaking my normal environment. To simplify the installation process I also modified the VSIX project to deploy to the experimental instance on build.

One word of warning about extensions in VS 2015. Extensions are installed as you’d expect but uninstall works a little differently. When an extension is uninstalled it is still left running and the folder structure remains. It isn’t until you restart VS that it cleans up uninstalled extensions. Therefore be sure to restart VS after uninstalling an extension otherwise you may run into issues if you try to, for example, reinstall the extension.