Visual Studio designer crash with inherited forms if the base form contains Revit API references

marco.giacomelliZSW69
Participant
Participant

Visual Studio designer crash with inherited forms if the base form contains Revit API references

marco.giacomelliZSW69
Participant
Participant

This is quite a weird problem with a lot of moving parts, I'm posting here first since I managed to reproduce this issue only with the Revit API, but also planning to try and bring this to a bug report to Microsoft.

 

This repository showcases an issue with Visual Studio and designing inherited forms that contain external DLL references, in this case Revit API.

In this project, frmIBase contains a reference to Revit and just creates an object from its library, then Form1 inherits from that form and doesn't do anything else, in this case the visual studio editor will crash with various messages, usually "Could not load file or assembly 'RevitAPIUI, Version=25.0.0.0, Culture=neutral, PublicKeyToken=null'." or

The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: Form1 --- The base class 'FormsCrash.frmIBase' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

 

I know Revit addins are not meant to run standalone and need to be hosted inside Revit, however it is normally possible to design the forms in visual studio, this can be observed by loading Form2 in the designer: that form has a direct reference, does not inherit from any other form, and loads properly in the designer.

This is not strictly a .NET 8 problem, looks like it's happening even on .net 4.8 and visual studio 2019

 

I made a small project that showcases the issue:

https://github.com/MaddoScientisto/FormsCrash-example

 

Short code samples:

Base Form:

 

public partial class frmIBase : Form
{
    private UIDocument Uidoc = new UIDocument(null);

    public frmIBase()
    {
        
    }
}

 

Inherited Form:

 

public partial class Form1 : frmIBase
{
    public Form1()
    {
        InitializeComponent();
    }
}

 

 Control Form that can be designed normally and does not inherit:

 

public partial class Form2 : Form
{
private UIDocument uidoc;

public Form2()
{
InitializeComponent();
}
}

 

 

I'm hoping someone ran into this issue and knows something about this, the obvious solution would be to not use inherited forms or use WPF, but that may not always be feasible, especially in the case of older codebases.

 

In the sample I'm using the Revit dlls from nice3point's nuget package, this issue happens even if the original dlls from the Revit installation folder are referenced directly, it even happens for Revit 2024 on .net 8

0 Likes
Reply
297 Views
4 Replies
Replies (4)

jeremy_tammik
Autodesk
Autodesk

It sounds to me as if you just need to ensure that Visual Studio has access to the required Revit API assembly DLL. Maybe the path is wrong, or incomplete?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

marco.giacomelliZSW69
Participant
Participant

I tried many things, usually I have a dll folder with the required dlls local to the repository but neither the nuget package nor pointing the hintpath to the Revit folder worked, ReferencePath is not supported in SDK projects and .net 8 as far as I know, are there other ways to ensure visual studio has access to that dll?

 

Mind that only inherited forms present the issue, non-inherited forms can locate the dll just fine and so does the addin at runtime in Revit

0 Likes

jeremy_tammik
Autodesk
Autodesk

How very weird indeed. Once the required DLL has been locates and loaded for one of the base forms, I would have expected it to be available for the other forms as well.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

marco.giacomelliZSW69
Participant
Participant

I could find a .net issue from 2022 related to inherited forms not being able to load referenced dlls: https://github.com/dotnet/winforms/issues/8287

That bug has been fixed in 2022 and in fact I could not reproduce the issue with a custom-made dll, only RevitApiUI does that

0 Likes