diff --git a/Samples/SampleProjects/LoadContext/Program.cs b/Samples/SampleProjects/LoadContext/Program.cs index 6275cd6..470c6e5 100644 --- a/Samples/SampleProjects/LoadContext/Program.cs +++ b/Samples/SampleProjects/LoadContext/Program.cs @@ -1,7 +1,6 @@ using System; using System.IO; using System.Linq; -using System.Reflection; using System.Runtime.Loader; using SharpDocx; @@ -12,11 +11,28 @@ internal class Program private static readonly string BasePath = Path.GetDirectoryName(typeof(Program).Assembly.Location) + @"/../../../../.."; - static AssemblyLoadContext LoadContext = new TestAssemblyLoadContext(Path.GetDirectoryName(typeof(Program).Assembly.Location)); + public static void Main() + { + ExecuteTemplates(out var loadContextRef); + + while (loadContextRef.IsAlive) + { + Console.WriteLine("Reference is still alive."); + //Console.WriteLine($"Loaded AssemblyCount: {AppDomain.CurrentDomain.GetAssemblies().Length}"); + //Thread.Sleep(1000); - private static void Main() + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + + Console.WriteLine("Document Assemblies have been unloaded."); + //Console.WriteLine($"Loaded AssemblyCount: {AppDomain.CurrentDomain.GetAssemblies().Length}"); + } + + private static void ExecuteTemplates(out WeakReference loadContextRef) { - DocumentFactory.LoadContext = LoadContext; + var loadCtx = new TestAssemblyLoadContext(Path.GetDirectoryName(typeof(Program).Assembly.Location)); + DocumentFactory.LoadContext = loadCtx; var viewPath = $"{BasePath}/Views/Tutorial.cs.docx"; var documentPath = $"{BasePath}/Documents/Tutorial.docx"; @@ -30,39 +46,18 @@ private static void Main() document.ImageDirectory = imageDirectory; document.Generate(documentPath); #endif + loadContextRef = new WeakReference(loadCtx); + Console.WriteLine("---------------------Assemblies Loaded In the Default Context-------------------------------"); var assemblyNames = AssemblyLoadContext.Default.Assemblies.Select(s => s.FullName).ToArray(); Console.WriteLine(string.Join(Environment.NewLine, assemblyNames)); Console.WriteLine("---------------------Assemblies Loaded In Context-------------------------------"); - assemblyNames = LoadContext.Assemblies.Select(s => s.FullName).ToArray(); + assemblyNames = loadCtx.Assemblies.Select(s => s.FullName).ToArray(); Console.WriteLine(string.Join(Environment.NewLine, assemblyNames)); - LoadContext.Unload(); + loadCtx.Unload(); DocumentFactory.LoadContext = null; - - Console.WriteLine("Document Assemblies have been unloaded."); - } - - class TestAssemblyLoadContext : AssemblyLoadContext - { - private readonly AssemblyDependencyResolver _resolver; - - public TestAssemblyLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true) - { - _resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath); - } - - protected override Assembly Load(AssemblyName name) - { - string assemblyPath = _resolver.ResolveAssemblyToPath(name); - if (assemblyPath != null) - { - return LoadFromAssemblyPath(assemblyPath); - } - - return null; - } } } } \ No newline at end of file diff --git a/Samples/SampleProjects/LoadContext/TestAssemblyLoadContext.cs b/Samples/SampleProjects/LoadContext/TestAssemblyLoadContext.cs new file mode 100644 index 0000000..ddd59a9 --- /dev/null +++ b/Samples/SampleProjects/LoadContext/TestAssemblyLoadContext.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.Loader; + +namespace LoadContext +{ + internal class TestAssemblyLoadContext : AssemblyLoadContext + { + private readonly AssemblyDependencyResolver _resolver; + + public TestAssemblyLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true) + { + _resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath); + } + + protected override Assembly Load(AssemblyName name) + { + string assemblyPath = _resolver.ResolveAssemblyToPath(name); + if (assemblyPath != null) + { + return LoadFromAssemblyPath(assemblyPath); + } + + return null; + } + } +} \ No newline at end of file