Skip to content

Commit

Permalink
hotfix r25 unload assemby
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Jun 29, 2024
1 parent 10b8aae commit 950bc6f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
23 changes: 18 additions & 5 deletions AddInManager/Command/AddinManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele
}
Result result = Result.Failed;
var alc = new AssemblyLoadContext(filePath);
Stream stream = null;
try
{
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
Assembly assembly = alc.LoadFromStream(stream);
object instance = assembly.CreateInstance(_activeCmdItem.FullClassName);
WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true);
Expand All @@ -121,13 +122,25 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele
}
catch (Exception ex)
{
// unload the assembly
MessageBox.Show(ex.ToString());
alc?.Unload();
result = Result.Failed;
WeakReference alcWeakRef = new WeakReference(alc, trackResurrection: true);
for (int counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++)
{
alc = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
stream?.Close();
Debug.WriteLine("Assembly unloaded");

}
finally
{
alc = null;
}
// finally
// {
// alc?.Unload();
// }
return result;
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('R25'))">
<RevitVersion>2025</RevitVersion>
<DebugType>Full</DebugType>
<DebugSymbols>true</DebugSymbols>
<TargetFramework>net8.0-windows</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<DefineConstants>$(DefineConstants);R25</DefineConstants>
Expand Down
6 changes: 3 additions & 3 deletions Test/TestEvaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class TestEvaCommand : IExternalCommand
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
MessageBox.Show("TestEvaCommand");
// string? value = DependLib.ShowDialogFolder();
// Console.WriteLine(value);
// MessageBox.Show(value.ToString());
string? value = DependLib.ShowDialogFolder();
Console.WriteLine(value);
MessageBox.Show(value.ToString());
return Result.Succeeded;
}
}

0 comments on commit 950bc6f

Please sign in to comment.