Skip to content

Commit

Permalink
Updated load context example to actually show unloading.
Browse files Browse the repository at this point in the history
  • Loading branch information
groogiam committed Sep 4, 2020
1 parent a0b7fb4 commit a36ece3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
53 changes: 24 additions & 29 deletions Samples/SampleProjects/LoadContext/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using SharpDocx;

Expand All @@ -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";
Expand All @@ -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;
}
}
}
}
26 changes: 26 additions & 0 deletions Samples/SampleProjects/LoadContext/TestAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit a36ece3

Please sign in to comment.