Use EnvDTE in a vsix extension to add a reference to a shared project in a multi-project template solution.

Leandro Teles 1 Reputation point
2021-08-01T02:54:48.293+00:00

I'm not sure if this is frowned upon, but I'm duplicating a question I asked in stackoverflow in the hopes of getting an answer to a situation I think is very obscure. I will update both with an answer when I have it.
Here is the question on stackoverflow

I have created a Visual Studio multi-project template in which one is a 'Shared' project, and another is a normal project for common 'Resources'. The other 5 normal projects each need to reference both the 'Shared' and the 'Resources' projects.

119613-image.png

On a separate solution I then create a VISX extension to implement a wizard, but instead of pointing the .vsixmanifest Asset to "A project in current solution", I point to the .zip multi-project template I created.

My intent is to then use the vsix wizard to add the necessary project references. I've already done so with envDTE, and it works beautifully... mostly.

A reference to the 'Resources' project is added without a problem. The issue is when trying to add a "shared" project as a reference. I've tried using both the (Project) and the (Filename.shproj) arguments.

    DTE VS = automationObject as DTE;  

    // This method is called after the solution is created.  
    public void RunFinished()  
        DTE VS = automationObject as DTE;  
    {  
        Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();  

        Project sharedProj = null;  
        Project resourceProj = null;  
        List<Project> versionProjs = new List<Project>();  
        foreach (Project project in VS.Solution.Projects)  
        {  
            if (project.Name.Contains("Resources")) { resourceProj = project; }  
            else if (project.Name.Contains("Shared")) { sharedProj = project; }  
            else { versionProjs.Add(project); }  
        }  

        foreach (Project version in versionProjs)  
        {  
            // Add reference to shared project  
            VSProject vsProj = (VSProject)version.Object;  
            if (sharedProj != null)  
            {  
                // !!! Shared project should be added as a reference here, but it is causing a critical error. !!!  

                //BuildDependency bldDepends = VS.Solution.SolutionBuild.BuildDependencies.Item(version.UniqueName);  
                //bldDepends.AddProject(sharedProj.FileName);  
                //vsProj.References.Add(sharedProj.FileName);  

                //vsProj.References.AddProject(sharedProj);  
            }  

            // Add reference to the Resources project  
            if (resourceProj != null) { vsProj.References.AddProject(resourceProj); }  
        }  
    }  

AddProject(Project) results in:

Exception thrown:'System.Runtime.InteropServices.COMException' in
TemplateExtension.dll

Catastrophic failure (Exception from HRESULT:
0x8000FFFF (E_UNEXPECTED))

AddProject(Filename.shproj) results in:

Exception thrown: 'System.Runtime.InteropServices.COMException' in
TemplateExtension.dll

Please make sure that the file is accessible,
and that it is a valid assembly or COM component.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,800 questions
{count} votes