question

LeandroTeles-6756 avatar image
0 Votes"
LeandroTeles-6756 asked NicolsSantostefano-9841 commented

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

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.


vs-extensions
image.png (4.6 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @LeandroTeles-6756
I cannot reproduce your issue. Can you provide a sample program?

0 Votes 0 ·

I have now made this project a public repository.

https://github.com/theBIMdev/RevitExtension
The problem code is commented out in lines 60-65 of Wizard.cs

0 Votes 0 ·

I have decided to make my project open to the public. The source code to this problem can be found here: https://github.com/theBIMdev/RevitExtension

The offending code specifically is in the Wizard.cs file. The commented out section represents 3 different attempts.
- Adding the project to the dependencies
- vsProj.References.Add(sharedProj.FileName);
- vsProj.References.AddProject(sharedProj);


0 Votes 0 ·

Hi, @LeandroTeles-6756
Thank you for your feedback. This issue has been escalated for further investigation.

0 Votes 0 ·

Hi, @LeandroTeles-6756 did you find the solution for adding a shared project reference? Im in the same situation.
I modify the .csproj file foreach revit version proj but after the vsix finish the files undo the modify lines. dont know why.
I use this code to add the common reference manually


  public void RunFinished()
          {
              if (!destination.EndsWith("Common"))
              {
                  var commonProject = string.Format("<Import Project=\"..\\{0}.Common\\{0}.Common.projitems\" " + "Label=\"Shared\" />", ext_projectname);
     
                  string fileName = destination + "\\" + projectname + ".csproj";
     
                  string text = File.ReadAllText(fileName);
                  text = text.Replace("<!--commonproject-->", commonProject);
                  File.WriteAllText(fileName, text);
              }
          }










0 Votes 0 ·
LeandroTeles-6756 avatar image LeandroTeles-6756 NicolsSantostefano-6977 ·

@NicolsSantostefano-6977 Unfortunately, not yet. I haven't had time to get back to this project yet, and VS2022 was not yet available when I ran into this issue. Once I get back to digging into this issue, I intend to first double check if the problem still persists when the extension is run on VS22.
Until then, if you receive any feedback or find a solution, please feel free to add it here.

Thanks!

0 Votes 0 ·
LeandroTeles-6756 avatar image LeandroTeles-6756 NicolsSantostefano-6977 ·

Hi @NicolsSantostefano-6977.
Still nothing yet. I've also not had the chance to get back to this project in quite some time, but I hope to dig into it again some time next month when I upgrade my vsix to VS20222.
Maybe the issue has been resolved by the new release of Visual Studio.

0 Votes 0 ·

Hi LeandroTeles-6756 i solve my problem by adding

Code in my WizardImplementation:

 public void ProjectFinishedGenerating(Project project)
         {
             Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
             project.Save();
         }

// This method is called after the project is created.
public void RunFinished()
{
string commonProject = string.Format("<Import Project=\"..\{0}.Common\{0}.Common.projitems\" " + "Label=\"Shared\" />",
ext_projectname);

         string fileName = destination + "\\" + projectname + ".csproj";

         FileHelper.ReplaceLineInTextFile(fileName, "<!--commonproject-->", commonProject);
     }


and in my .csproj templates files I have a comment inside <!--commonproject--> so my wizard change this comment with the line corresponding to the reference of the common project then save the project in the ProjectFinishedGenerating method









0 Votes 0 ·

0 Answers