Using Nuget in a Visual Studio Project Template

Topics: General
Oct 19, 2013 at 9:22 AM
Hello all,
I am currently trying to build a Visual Studio Project Template that uses the UmbracoCMS Nuget package.

I have tried this method as outlined here

However from what I can tell this method does not allow me to include the main UmbracoCMS Nuget package in the <packages> XML and then get all it's dependencies.

Would I be right in thinking that this would require me to walk through each package and figure out their dependencies and manually include them in the <packages> XML along with including them in the .VISX extension?

So I looked into another approach and saw this blog post on Nuget

So my idea was to use the Nuget.Core C# API to fetch the latest Umbraco Nuget package and use that in the project.

I have got so far with it & have managed to successfully fetch the packages & its dependencies and store them in the /packages folder.

The problem I am having is adding the actual Nuget contents & it's DLL references to the project itself.

I am trying to use ProjectManager.AddPackageReference() method however I cannot get it to work, so I presume I am using it wrong as the two events I wired up to it PackageReferenceAdding & PackageReferenceAdded do not fire at all when stepping through the code.

Here is a copy of my Wizard class I am using in my VS Project Template/VISX
I have highlighted the section where I am trying to insert the Nuget Package into the project

I would love any advice on how best to add the UmbracoCMS Nuget package & its dependencies into the project.

Many Thanks,
Warren :)
Oct 21, 2013 at 2:45 PM
Ok just an update on progress.
I have seen another approach on how I should be doing this with the MEF services as mentioned on the docs site for NuGet.

Currently I am having an issue getting the variable componentModel as mentioned in the example.
This line from the docs site does not compile & I am unsure which reference/s I am missing for it to compile
var componentModel = (IComponentModel)GetService(typeof(SComponentModel));

I have seen another discussion on here that is using it however I have tried other approaches as suggested & it's returning null.
So I am obviously doing something wrong

Tried both of these and still no luck
//var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
var componentModel = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel;

Can anyone give me any pointers on how to resolve this please.

Warren :)
Oct 21, 2013 at 3:57 PM
Hi Warren,

What's the problem with using the first approach - the <packages> element?

Oct 21, 2013 at 4:07 PM
Hey Benjamin,
Using the <packages> xml approach is fine but requires Umbraco & all of its dependent NuGet packages to also be bundled into the VISX, which would be hard for me to then mantain myself and would require the VISX to be updated evertytime Umbraco does a release & any of the dependant packages.

Hence I am looking into using the NuGet API to fetch the .nupkg & its dependencies which I got working fine it's just getting this componentModel variable which allows me to use specific services to then install the NuGet into the VS project.
Oct 21, 2013 at 5:07 PM
You should be able to implement the RunStarted method:

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{ = new ServiceProvider(automationObject as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

You should then have an instance of the IServiceProvider at which point you can call:



Oct 22, 2013 at 8:41 AM
Hey Benjamin,
I tried your suggestion and I must be doing something very wrong & dumb here.
Saw your Skype request from the other day, maybe if you dont mind & have some time are able to help me find out what the problem is later on.

Warren :)
Oct 22, 2013 at 6:17 PM
We don't give you a good way to have NuGet determine the dependencies for you instead of having to declare them yourself. We do have an open work item to allow that through the API for installing preinstalled packages, and it sounds like your request is to also allow that for VS template-specified packages.

The reason we don't presently offer that is based on the idea that tooling should be installing a Static list of packages (totally deterministic), and therefore it's far better to have the tooling developer do a little work to get the full list of packages and then hard-code it, than to have NuGet have to do a dependency walk every time the tooling gesture (or template) is executed, as that will slow down the tooling execution.

If we allowed an attribute on the <package> element that allowed you to say ignoreDependencies="false" and then walked the dependencies, installing them from the same local repository as the specified package, would that be good for you?
Oct 22, 2013 at 9:02 PM
Hello Jeff,
Yes to have an ignoreDependencies attribute on the XML for the <package> would be fantastic.

I understand your reasoning for setting the specific NuGet packages manually, but for the Umbraco project I think we are talking 15-20 NuGet packages to add if not more and every time Umbraco does a new NuGet release I will need to update the VISX & triple check all the dependencies and is prone to my human error.

Hence I have been looking into the extensibility APIs to ensure it gets the latest 'stable' Umbraco NuGet package available that way I will only have to release one VISX and not update it too often every time Umbraco releases a new NuGet release.

But in the meantime are you able to offer any advice or pointers on why ComponentModel variable is null?

Many Thanks,
Oct 26, 2013 at 7:48 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Marked as answer by JeffHandley on 10/26/2013 at 11:50 AM