Skip to content

Commit

Permalink
resolve dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Apr 17, 2024
1 parent 5f79b23 commit 9c32f1a
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 8 deletions.
28 changes: 28 additions & 0 deletions AddInManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RevitElementBipChecker", "RevitElementBipChecker\RevitElementBipChecker.csproj", "{21460D85-C4AD-49D5-963F-CF13C4AE99EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test2", "Test2\Test2.csproj", "{DA609427-F086-4C79-A5FA-202DBB5DE48D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug R22|Any CPU = Debug R22|Any CPU
Expand Down Expand Up @@ -94,6 +96,8 @@ Global
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release R24|Any CPU.ActiveCfg = Release R24|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release R25|Any CPU.ActiveCfg = Release R25|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release R25|Any CPU.Build.0 = Release R25|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R25|Any CPU.ActiveCfg = Release R25|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug R25|Any CPU.Build.0 = Release R25|Any CPU
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R22|Any CPU.Build.0 = Debug R22|Any CPU
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Installer|Any CPU.ActiveCfg = Debug R22|Any CPU
Expand All @@ -118,6 +122,30 @@ Global
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Release R25|Any CPU.Build.0 = Release R25|Any CPU
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R25|Any CPU.ActiveCfg = Release R25|Any CPU
{21460D85-C4AD-49D5-963F-CF13C4AE99EB}.Debug R25|Any CPU.Build.0 = Release R25|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R22|Any CPU.Build.0 = Debug R22|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R23|Any CPU.ActiveCfg = Debug R23|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R23|Any CPU.Build.0 = Debug R23|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R24|Any CPU.ActiveCfg = Debug R24|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R24|Any CPU.Build.0 = Debug R24|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R25|Any CPU.ActiveCfg = Debug R21|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Debug R25|Any CPU.Build.0 = Debug R21|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Installer|Any CPU.ActiveCfg = Debug R21|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Installer|Any CPU.Build.0 = Debug R21|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R19|Any CPU.ActiveCfg = Release R19|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R19|Any CPU.Build.0 = Release R19|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R20|Any CPU.ActiveCfg = Release R20|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R20|Any CPU.Build.0 = Release R20|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R21|Any CPU.ActiveCfg = Release R21|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R21|Any CPU.Build.0 = Release R21|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R22|Any CPU.ActiveCfg = Release R22|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R22|Any CPU.Build.0 = Release R22|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R23|Any CPU.ActiveCfg = Release R23|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R23|Any CPU.Build.0 = Release R23|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R24|Any CPU.ActiveCfg = Release R24|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R24|Any CPU.Build.0 = Release R24|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R25|Any CPU.ActiveCfg = Release R25|Any CPU
{DA609427-F086-4C79-A5FA-202DBB5DE48D}.Release R25|Any CPU.Build.0 = Release R25|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
21 changes: 20 additions & 1 deletion AddInManager/Command/AddinManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,24 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele
return 0;
}
Result result = Result.Failed;
var alc = new AssemblyLoadContext();
var alc = new AssemblyLoadContext(filePath);
try
{

// get list dependencies assembly at same folder
//var dependencies = alc.GetDependencies(filePath);
// List<AssemblyLoadContext> subAlcs = new List<AssemblyLoadContext>();
// foreach (var dependency in dependencies)
// {
// string subFilePath = Path.Combine(Path.GetDirectoryName(filePath), dependency + ".dll");
// var subAlc = new AssemblyLoadContext(subFilePath);
// if (!File.Exists(subFilePath)) continue;
// // load stream with security permission
// // var subStream = new FileStream(subFilePath, FileMode.Open, FileAccess.Read);
// subAlc.LoadFromAssemblyPath(subFilePath);
// // subStream.Close();
// subAlcs.Add(subAlc);
// }
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
Assembly assembly = alc.LoadFromStream(stream);
object instance = assembly.CreateInstance(_activeCmdItem.FullClassName);
Expand All @@ -109,6 +124,10 @@ public Result RunActiveCommand(ExternalCommandData data, ref string message, Ele
_activeEc = externalCommand;
result = _activeEc.Execute(data, ref message, elements);
alc.Unload();
// foreach (AssemblyLoadContext subAlc in subAlcs)
// {
// subAlc.Unload();
// }
}
int counter = 0;
for (counter = 0; alcWeakRef.IsAlive && (counter < 10); counter++)
Expand Down
42 changes: 39 additions & 3 deletions AddInManager/Model/AssemblyLoadContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


#if R25
#if R25
using System.IO;
using System.Reflection;
using System.Runtime.Loader;

Expand All @@ -10,7 +9,44 @@ class AssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext
{
public AssemblyLoadContext() : base(isCollectible: true)
{
}
private AssemblyDependencyResolver _resolver;

public AssemblyLoadContext(string pluginPath): base(isCollectible: true)
{
_resolver = new AssemblyDependencyResolver(pluginPath);
}

protected override Assembly Load(AssemblyName assemblyName)
{
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
if(assemblyPath.Contains("RevitAPI"))
{
return null;
}
if(assemblyPath.Contains("RevitAPIUI"))
{
return null;
}
var stream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read);
return LoadFromStream(stream);
}

return null;
}

protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}

return IntPtr.Zero;
}

}
#endif
3 changes: 3 additions & 0 deletions Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@
<ItemGroup>
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Test2\Test2.csproj" />
</ItemGroup>

</Project>
11 changes: 7 additions & 4 deletions Test/TestEvaCommand.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Windows;
using System;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Test2;

namespace Test;

Expand All @@ -10,9 +12,10 @@ public class TestEvaCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

UIApplication uiApplication = commandData.Application;
MessageBox.Show(uiApplication.Application.VersionBuild);
MessageBox.Show("TestEvaCommand");
double plus = DependLib.Plus();
Console.WriteLine(plus);
MessageBox.Show(plus.ToString());
return Result.Succeeded;
}
}
10 changes: 10 additions & 0 deletions Test2/DependLib.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Test2
{
public static class DependLib
{
public static double Plus()
{
return 2 + 6;
}
}
}
38 changes: 38 additions & 0 deletions Test2/Test2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<PlatformTarget>x64</PlatformTarget>
<UseWpf>true</UseWpf>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('Debug'))">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DefineConstants>$(DefineConstants);DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('Release'))">
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<Version>$(RevitVersion)</Version>
<EnableDynamicLoading>true</EnableDynamicLoading>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Description>A Project Support for developer in revit </Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Chuongmep.Revit.Api.RevitAPI" Version="$(RevitVersion).*" />
<PackageReference Include="Chuongmep.Revit.Api.RevitAPIUI" Version="$(RevitVersion).*" />
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationFramework" />
</ItemGroup>

</Project>

0 comments on commit 9c32f1a

Please sign in to comment.