Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
class TestAssemblyLoadContext : AssemblyLoadContext
{
private AssemblyDependencyResolver _resolver;
using System.Reflection;
using System.Runtime.Loader;

public TestAssemblyLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true)
namespace complex
{
class TestAssemblyLoadContext : AssemblyLoadContext
{
_resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath);
}
private AssemblyDependencyResolver _resolver;

protected override Assembly Load(AssemblyName name)
{
string assemblyPath = _resolver.ResolveAssemblyToPath(name);
if (assemblyPath != null)
public TestAssemblyLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true)
{
return LoadFromAssemblyPath(assemblyPath);
_resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath);
}

return null;
protected override Assembly Load(AssemblyName name)
{
string assemblyPath = _resolver.ResolveAssemblyToPath(name);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}

return null;
}
}
}
94 changes: 49 additions & 45 deletions samples/snippets/standard/assembly/unloading/simple_example.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;

// <Snippet1>
class TestAssemblyLoadContext : AssemblyLoadContext
namespace simple
{
public TestAssemblyLoadContext() : base(isCollectible: true)
// <Snippet1>
class TestAssemblyLoadContext : AssemblyLoadContext
{
}
public TestAssemblyLoadContext() : base(isCollectible: true)
{
}

protected override Assembly Load(AssemblyName name)
{
return null;
protected override Assembly Load(AssemblyName name)
{
return null;
}
}
}
// </Snippet1>
// </Snippet1>

class Test
{
// <Snippet2>
[MethodImpl(MethodImplOptions.NoInlining)]
static int ExecuteAndUnload(string assemblyPath, out WeakReference alcWeakRef)
class Test
{
// <Snippet3>
var alc = new TestAssemblyLoadContext();
Assembly a = alc.LoadFromAssemblyPath(assemblyPath);
// </Snippet3>

alcWeakRef = new WeakReference(alc, trackResurrection: true);
// <Snippet2>
[MethodImpl(MethodImplOptions.NoInlining)]
static int ExecuteAndUnload(string assemblyPath, out WeakReference alcWeakRef)
{
// <Snippet3>
var alc = new TestAssemblyLoadContext();
Assembly a = alc.LoadFromAssemblyPath(assemblyPath);
// </Snippet3>

// <Snippet4>
var args = new object[1] { new string[] {"Hello"}};
int result = (int)a.EntryPoint.Invoke(null, args);
// </Snippet4>
alcWeakRef = new WeakReference(alc, trackResurrection: true);

// <Snippet5>
alc.Unload();
// </Snippet5>
// <Snippet4>
var args = new object[1] {new string[] {"Hello"}};
int result = (int) a.EntryPoint.Invoke(null, args);
// </Snippet4>

return result;
}
// </Snippet2>
// <Snippet5>
alc.Unload();
// </Snippet5>

static void ExecuteAssemblyInUnloadableContext()
{
// <Snippet6>
WeakReference testAlcWeakRef;
int result = ExecuteAndUnload("absolute/path/to/your/assembly", out testAlcWeakRef);
// </Snippet6>
return result;
}
// </Snippet2>

// <Snippet7>
for (int i = 0; testAlcWeakRef.IsAlive && (i < 10); i++)
static void ExecuteAssemblyInUnloadableContext()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
// </Snippet7>
// <Snippet6>
WeakReference testAlcWeakRef;
int result = ExecuteAndUnload("absolute/path/to/your/assembly", out testAlcWeakRef);
// </Snippet6>

// <Snippet7>
for (int i = 0; testAlcWeakRef.IsAlive && (i < 10); i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
// </Snippet7>

bool success = testAlcWeakRef.IsAlive;
bool success = testAlcWeakRef.IsAlive;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace test
{
Expand All @@ -12,7 +13,7 @@ class Program
{
public static void ThreadProc()
{
// Issue preventing unlopading #4 - a thread running method inside of the TestAssemblyLoadContext at the unload time
// Issue preventing unloading #4 - a thread running method inside of the TestAssemblyLoadContext at the unload time
Thread.Sleep(Timeout.Infinite);
}

Expand Down
9 changes: 9 additions & 0 deletions samples/snippets/standard/assembly/unloading/unloading.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<StartupObject>test.Program</StartupObject>
</PropertyGroup>

</Project>