0

I created one console application to create visual studio project pragmatically, here i am not able install Nuget packages, always var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); statement returns null values. for your reference i added my code below. Help me to resolve this issue.

static void Main(string[] args)
        {
            //InstallNuGetPackages.InstallNuGet("");
            string ProjectName = "WebAPIProj";
            string SolutionName = "EmptyTemplate";
            System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
            Object obj = System.Activator.CreateInstance(type, true);
            EnvDTE.DTE dte = (EnvDTE.DTE)obj;
            dte.MainWindow.Visible = true; // optional if you want to See VS doing its thing

            // create a new solution
            dte.Solution.Create("C:\\"+ SolutionName + "\\", SolutionName);
            var solution = dte.Solution;

            // create a C# WinForms app
            solution.AddFromTemplate(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ProjectTemplatesCache\CSharp\Web\1033\EmptyWebApplicationProject40\EmptyWebApplicationProject40.vstemplate",
                @"C:\NewSolution\"+ ProjectName, ProjectName);
            InstallNuGetPackages.InstallNuGet(dte);

            foreach (var p in dte.Solution.Projects)
            {
              InstallNuGetPackages.InstallNuGet((Project)p, "Microsoft.AspNet.WebApi version1.1");
            }

            // save and quit
            dte.ExecuteCommand("File.SaveAll");
            dte.Quit();
        }

Code to install Nuget Packages

 public bool InstallNuGetPackage(Project project, string package)
    {
        bool installedPkg = true;
        try
        {
            var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); //Always this statement returns null
            IVsPackageInstallerServices installerServices = componentModel.GetService();
            if (!installerServices.IsPackageInstalled(project, package))
            {
                var installer = componentModel.GetService();
                installer.InstallPackage(null, project, package, (System.Version)null, false);
            }
        }
        catch (Exception ex)
        {
            installedPkg = false;
        }
        return installedPkg;
    }
Bakudan
  • 19,134
  • 9
  • 53
  • 73
Gopinath2105
  • 35
  • 1
  • 7
  • why are you doing this? have you looked to [Custom templates](https://github.com/dotnet/docs/blob/master/docs/core/tools/custom-templates.md) ? – DaniCE Mar 15 '18 at 15:23
  • i have to create many WebAPI project, to reduce manual coding, i choosed this way. – Gopinath2105 Mar 15 '18 at 15:25
  • If you can target dot net core 2 the CLI new command "[dotnet new webapi](https://github.com/dotnet/docs/blob/master/docs/core/tools/dotnet-new.md#net-core-2x-1)" creates just that. – DaniCE Mar 15 '18 at 15:34

1 Answers1

0

(Turned this into an answer for better readability and more room)

created one console application - you only have access to the ServiceProvider from Visual Studio if you run your code inside of it, i.e. from an extension and/or package.

Running this from a console application cannot work. Visual Studio internally does a lot more setup for all the services and general environment than creating an instance of DTE.

To persue your route, although I'm not sure how feasible that is, invoke nuget.exe or NuGet.Core code to achieve similar.

Christian.K
  • 47,778
  • 10
  • 99
  • 143