Skip to content

Commit

Permalink
load context aseembly without copy
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Apr 17, 2024
1 parent 129bb81 commit b7d7d9f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
28 changes: 6 additions & 22 deletions AddInManager/Command/AddinManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using RevitAddinManager.ViewModel;
using System.Windows;
using static RevitAddinManager.App;
using AssemblyLoadContext = RevitAddinManager.Model.AssemblyLoadContext;

namespace RevitAddinManager.Command;

Expand Down Expand Up @@ -43,16 +44,15 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele
return 0;
}
Result result = Result.Failed;
var alc = new TestAssemblyLoadContext();
var alc = new AssemblyLoadContext();
try
{
Trace.WriteLine("Loading assembly...");
Assembly assembly = alc.LoadFromAssemblyPath(filePath);
var 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);
if (instance is IExternalCommand externalCommand)
{
Trace.WriteLine("Chuong");
_activeEc = externalCommand;
result = _activeEc.Execute(data, ref message, elements);
alc.Unload();
Expand All @@ -61,39 +61,23 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele
for (counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++)
{
alc = null;
Console.WriteLine("Waiting for unload...");
GC.Collect();
GC.WaitForPendingFinalizers();
}
//TODO : Why?>???????? still can't delete inside Revit ^_^
Console.WriteLine("Try Delete");
File.Delete(filePath);
if(File.Exists(filePath))
{
Console.WriteLine("Delete Not Done");
}
stream.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.ToString());
result = Result.Failed;
}
finally
{
Trace.WriteLine("Unloading context...");
alc.Unload();
int counter = 0;
alc = null;
}
return result;
}


private void AssembUnloading(AssemblyLoadContext obj)
{
Console.WriteLine("Unloading.....");
}

public static AddinManagerBase Instance
{
get
Expand Down
12 changes: 12 additions & 0 deletions AddInManager/Model/AssemblyLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Reflection;
using System.Runtime.Loader;

namespace RevitAddinManager.Model;

class AssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext
{
public AssemblyLoadContext() : base(isCollectible: true)
{

}
}
12 changes: 0 additions & 12 deletions AddInManager/Model/TestAssemblyLoadContext.cs

This file was deleted.

2 changes: 1 addition & 1 deletion AddInManager/RevitAddinManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
<AddinFiles Include="$(RootDir)**\*.*" />
</ItemGroup>
<Message Text="Copy files to Addin folder" Importance="high" />
<Copy SourceFiles="@(AddinFiles)" DestinationFolder="$(AppData)\Autodesk\Revit\Addins\$(RevitVersion)\%(RecursiveDir)" Condition="$(Configuration.Contains('Release'))" />
<Copy SourceFiles="@(AddinFiles)" DestinationFolder="$(AppData)\Autodesk\Revit\Addins\$(RevitVersion)\%(RecursiveDir)" Condition="$(Configuration.Contains('Debug'))" />
</Target>

</Project>
2 changes: 1 addition & 1 deletion Test/TestEvaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
{

UIApplication uiApplication = commandData.Application;
MessageBox.Show(uiApplication.Application.Username);
MessageBox.Show(uiApplication.Application.VersionBuild);
return Result.Succeeded;
}
}

0 comments on commit b7d7d9f

Please sign in to comment.