1

I am using the following code to try to programmatically create Visual Studio 2015 projects on the fly from within an existing Console Application. Why you ask? We are putting together a library of all our code snippets and we want a test/proof test solution for each one, and since we have hundreds of these, we aren't going to do it manually.

Ultimately this will be in an MVC5 app as its own project (class library?) but for now we are just trying to get it functional within this console application.

I am trying to create a new solution with 2 projects (a console application and a unit test project).I am also not even sure I need the unit test project, or whether one even works with a console app since there is no option to add a unit test to a console app in VS2015 solution generation.

Here is the code;

public class CreateConsoleProjectsProgrammatically
{
    private const string Vs2015Type = "VisualStudio.DTE.14.0";
    private const string Vs2015ConsoleProject = @"X:\Code Library\Helpers\ConsoleApplication\csConsoleApplication.vstemplate";
    private const string Vs2015UnitTestProject = @"X:\Code Library\Helpers\UnitTestProject\UnitTestProject.vstemplate";
    private const string Vs2015CodeLibraryBasepath = @"X:\Code Library";

    public static void CreateVsConsoleProjectProgrammatically(string filename)
    {
        // Create a solution with two projects in it, based on project
        // templates, a console project and a unit test project.
        var vsType = Type.GetTypeFromProgID(Vs2015Type);

    //error line is below
        var vsInstance= Activator.CreateInstance(vsType, true);

        EnvDTE.DTE dte = (EnvDTE.DTE)vsInstance;
        dte.MainWindow.Visible = false; // optional: set to true if you want to see VS doing its thing

        // create a new solution
        dte.Solution.Create(@"X:\Code Library\", filename);
        var solution = dte.Solution;

        // create a C# console application
        solution.AddFromTemplate(Vs2015ConsoleProject,Path.Combine(Vs2015CodeLibraryBasepath,filename), filename);

        // create a unit test project
        solution.AddFromTemplate(Vs2015UnitTestProject, Path.Combine(Vs2015CodeLibraryBasepath, filename + "_Test"), filename + "_Test");

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

Here is the error which is from the Activator.CreateInstance

Creating an instance of the COM component with CLSID {A2FA2136-EB44-4D10-A1D3-6FE1D63A7C05} from the IClassFactory failed due to the following error: 8001010a The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)).

Not sure why I am getting a server busy error. It almost seems like its because VS is already open? But how else would the new solution be generated? I tried closing the solution and reopening it but that did nothing to solve the issue.

I also thought it may have been a connectivity issue, so I moved all the files and directories to C:\ but it produced the same error, so it wasn't an issue of using a networked drive location.

I also tried using

EnvDTE80.DTE2 dte2 = (EnvDTE100.DTE2)vsInstance;

but DTE2 is not an available property/method on EnvDTE100 according to the editor, even though I found some examples using it on the net.

dinotom
  • 4,990
  • 16
  • 71
  • 139
  • Put the [STAThread] attribute on your Main() method and try again. Do document long delays you see, anything taking longer than 60 seconds is often an environmental problem. Like a disconnected network share. – Hans Passant Sep 04 '16 at 14:48
  • @Hans Passant... that probably won't solve the issue as i noted in the post I moved everything to the local drive C:\ to see if the X:\ networked drive was the issue...and running the project while I typed the prior text gave the error, RPC_E_CALL_REJECTED, with main method having that attribute. – dinotom Sep 04 '16 at 15:12
  • Not enough cues to help us help you. Next step is to [call CoRegisterMessageFilter](https://blogs.msdn.microsoft.com/andreww/2008/11/19/implementing-imessagefilter-in-an-office-add-in/) to tell VS to keep trying. – Hans Passant Sep 04 '16 at 15:21
  • @Hans Passant... I tried implementing this solution, found here...http://stackoverflow.com/questions/20892690/using-c-how-do-i-create-a-new-visual-studio-2012-solution-programmatically but that did not help, I get the same errors – dinotom Sep 04 '16 at 15:59

0 Answers0