Using NuGet packages in Visual Studio 2012 project templates

Anyone creating Visual Studio project templates these days should be using NuGet package references for project dependencies — it saves you having to update your templates every time a dependency changes, and follows a standard & generally accepted binary dependancy approach.

If you’re distributing your project template as a VSIX package (strongly recommended) the NuGet docs here specify the preferred approach is to include the .nupkg files within the VSIX, but the instructions specify the addition of a ‘CustomExtension’ element to the .vsixmanifest file that is no longer valid in v2 of the VSIX schema (the default in Visual Studio 2012). I spent a considerable period of time attempting to work out what the v2 equivalent of CustomExtension was, but to cut a long story short, you don’t need to make any changes to the .vsixmanifest — it’s enough to include all of the packages in the VSIX under a ‘Packages’ directory.

Following are the steps I used to create a working 2012 VSIX project template:

  1. Download and install the Visual Studio 2012 SDK, if you haven’t done so already.
  2. Create a solution and add a ‘C# Project Template’ project and a ‘VSIX Project’ project (under ‘Visual C# > Extensibility’). It amuses me no end that there’s a project template project template. I am easily amused though.
  3. Set up your project template the way you want it. I find it easier to create a temporary project, use ‘File > Export Template…’ and then unzip the package and copy the relevant bits across.
  4. Update your .vstemplate with the ‘WizardExtension’ and ‘WizardData’ elements like the following:
      <WizardExtension>
        <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
        <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
      </WizardExtension>
      <WizardData>
        <packages repository="extension" repositoryId="<!-- your VSIX Product Id -->">
          <!-- your NuGet package dependencies go here, eg: -->
          <package id="Moq" version="4.0.10827" />
          <package id="xunit" version="1.9.1" />
          <package id="xunit.extensions" version="1.9.1" />
        </packages>
      </WizardData>
    
  5. Double-click on the source.extension.vsixmanifest to open it in the designer, and add a new Asset. Select ‘Microsoft.VisualStudio.ProjectTemplate’ as the type, “a project in the current solution” as the source, and then choose your template project. See this blog post for more info about this approach.
  6. Create a folder under your VSIX project called ‘Packages’ and drag the .nupkg files (referenced in the .vstemplate above) into this folder. Set the Build Type on the files to ‘Content’ and ‘Include in VSIX’ to ‘True’.
  7. Build the solution – it will produce a VSIX that includes the NuGet dependencies. You’re done!

It’s more correct to also add the NuGet Package Manager extension as a dependency to your VSIX, although I don’t usually bother for our internal templates.

One of the great things about VSIX distribution is that you can add multiple project & item templates to a single package as a discrete, easily distributable approach (just add more assets to the vsixmanifest).

Advertisements

11 thoughts on “Using NuGet packages in Visual Studio 2012 project templates

  1. Hi Sam,

    I am wanting to extend the NuGet template wizard so I can do everything as per your post (use the NuGet.VisualStudio.TemplateWizard to pull in the packages from nuget) aswell as perform my own custom parameter substitutions. Do you know if this is possible? I’ve successfully written my wizard and it works nicely. Now I want to combine the two, and as far as I can tell the vstemplate only supports one wizard extension. So, I’d like use a decorator type pattern where my wizard simply wraps the nuget wizard and then performs some extra stuff and uses the WizardData as per your scenario.

    Any suggesions?
    Mark.

    1. Hi Mark,

      No, I haven’t done any work with the template wizard, sorry. Wrapping the Nuget Template Wizard sounds like it might work though, otherwise you’d probably have to manually reimplement everything it’s doing. Let me know how you get on.

      1. Thanks for your response Sam. I managed to accomplish what I was after by using a little bit of reflection in my custom wizard to instantiate the TemplateWizard class via constructor info (with non-public binding flags set because it is an internal sealed class), and then delegating all method calls to this instance.

        This way I only had to switch out the WizardExtension XML element for my wizard implementation and the WizardData element remained the same as you described. Worked a treat!

  2. Thanks for this. Looking at the sample helped me get my bearings. It would be great to see an updated version for VSIX in VS 2012, which is quite different than the prior version.

  3. Hello
    I created a vsix just like you said. However, my project template does not appear in “file -> New project -> Web”. I can see my template if I go to “Tools -> Extensions and Updates”. Can u help pls?
    Thx
    -Teeboy

  4. > It’s more correct to also add the NuGet Package Manager extension as a dependency to your VSIX, although I don’t usually bother for our internal templates

    How would one go about doing this?

      1. Is it possible to automatically download the missing nuget packages when the project is created?

        At the moment, I have to manually “Enable package restore” for the solution and build the solution for the packages to be installed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s