Nuget.VisualStudio from a VSIX extension

Topics: General
Nov 9, 2012 at 7:20 AM

hi! my goal is to write an extension (in fact, a guidance automation package) to generate a solution, with some projects, some of them using nuget packages.

i must be able to:

- add a source, with credentials

- add packages to project, without knowing from which source. (like in nuget.exe install, you don't have to specifiy the source, if so, it browses all sources)


so i try Nuget.VisualStudio first with success:

- IVsPackageInstaller to install packages (overload with repository, passing an aggregate repository --> needed to reference Nuget.Core)

- IVsPackageSourceProvider to add sources, but again, needed to reference Nuget.Core to access to PackageSource type


it worked beyond my hopes, untill for some reasons i had my Nuget Extension using different dlls versions from the ones I reference from my package project.

the problem is Nuget.VisualStudio will never change (I hope :-) ) but Nuget.Core version always changes.

so, even if I solved the version mismatch problem on my machine, I can't force users to have a certain Nuget version.

my thoughts:

- find a way not to used Nuget.Core, but onlu Nuget.VisualStudio

- find a way to install to a package to a specific project, using nuget.exe

- find a way to import Nuget command in a RunSpace, to be able to "nuget powershell" from my C# code

Nov 9, 2012 at 7:32 AM

I forgot to tell:

- I already tried this way: but it does not work with guidance packages: it uses a special serializer for TemplateData that does not support "packages" tag

- I must be able to do it by code, because I have to create menu entries that enable user to add packages

- when packages are installed, they must execute their powershell script, so I don't think (maybe i'm wrong?) that enabling package restore will work. It would also force me to add references to my *.csproj.template for the assemblies of the package (again, maybe I'm wrong)

Nov 9, 2012 at 10:44 AM

Have you read this blog post?

You must not reference NuGet.Core, because its version will change as you've found out. NuGet.VisualStudio also changes but because it allows you to reference using No PIA option, you don't have the problem when user upgrades NuGet.

In short, if you follow the instructions in the blog post, you should be fine.

One last note, for the IVsPackageInstaller.InstallPackage() method, if you pass "All" to the 'source' parameter, it will use the aggregate source.


Nov 9, 2012 at 11:11 AM

a great thanks for the link!

it solves some of my issues with Nuget.Core, but I still need a way to add a package source.

when getting an IVsPackageSourceProvider service by calling "ServiceLocator.GetInstance<IVsPackageSourceProvider>()", I can't access to "LoadPackageSources" and "SavePackageSources" because it uses PackageSource defined in Nuget.Core.

I also have a problem with "HttpClient.DefaultCredentialProvider" with is set to "NullCredentialProvider" and so I can't access to my private Nuget feed which require authentication.

I will post in the link you provided to me.


again, thanks!


Nov 9, 2012 at 11:28 AM

Yes, unfortunately, we don't expose any API to allow you to manage package sources. Can you describe what are you trying to do? I'm concerned that allowing extensions to modify the package sources arbitrarily without user's consent may not be the best option here.

NuGet handles authentication automatically when a feed requires authentication. It prompts user to enter the credentials. Doesn't it work for your case?

Nov 9, 2012 at 12:59 PM

I am currently creating a guidance package for my own company: it will be used by my colleagues.

most of them even don't know nuget exists (yes, it's possible ;-) ) and the goal is to "bother" the less possible them with feed url, password, etc....

we plan to buy a nuget host plan (pay monthly not to bother about feed hosting) and the service we decided to subscribe to need authentication.


the goal is that my colleagues have only to install my VSIX and "that's all": no configuration at all (feed url, password), no dialog boxes in whhich you have to fill anything.

maybe I can launch nuget.exe for example on the VSIX installation? or at each use of my guidance package?

what would be your suggestions?

Nov 9, 2012 at 5:37 PM

You could try and create the right sort of NuGet.config file under %AppData%\NuGet\ which contains all the requisite values. We do aggressively cache some of the settings, so you may need to tinker around with that, but it should work otherwise. 

Nov 9, 2012 at 5:43 PM

What you can do is create a batch file that does two things: invoke the VSIX installer to install your VSIX and call nuget.exe to add the new package source as well as set the credentials for it. I actually did the same thing myself for the internal nuget gallery at Microsoft here.

Nov 9, 2012 at 6:00 PM

would tou mind sharing your batch file? without your credential, of course :-)

I could do some research to write it myself, but it would save me some time.