Skip to content

Test loading and unloading assemblies in .NET 6 with AssemblyLoadContext

Notifications You must be signed in to change notification settings

Seekatar/assemblyUnloadTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

.NET Core Assembly Unloading

This is the source code for this blog entry about unloading assemblies in .NET.

Program.cs

This console app loads, calls, and unloads assemblies in an AssemblyLoadContext. The following commands are available in Program.cs.

Key Action
a Load libA library, built separately, into a context
b Load libB library, built separately, into a context
c Call Message() on all loaded assemblies
d Dump info about the contexts
g Call garbage collector
p Call Value() with 1-50 and plot results for all loaded assemblies
e Enter an equation to dynamically build and loaded assembly
x Build and load 1 assembly
y Build and load 100 assemblies
z Build and load 1000 assemblies
u Unload all assemblies
q Quit

For most of the commands, if you enter the capital letter it will use a "Test" load context. So there can be up to three contexts: Default and two created by the test app.

For e you are prompted for a simple math expression. System.Math methods are available without a prefix (e.g. Sin(d)). The expression is then used to pass to Rosyln to build and load the assembly on-the-fly. As a fun little exercise, pressing p plots them using Ascii Chart C#.

>>>> Chart for Sin(d/4)
  1.00 ┤    ╭───╮                    ╭───╮
  0.80 ┤   ╭╯   ╰╮                  ╭╯   ╰╮
  0.60 ┤  ╭╯     ╰╮                ╭╯     ╰╮
  0.40 ┤ ╭╯       ╰╮              ╭╯       ╰╮
  0.20 ┤╭╯         ╰╮            ╭╯         ╰╮
 -0.00 ┼╯           │           ╭╯           ╰╮
 -0.20 ┤            ╰╮         ╭╯             │
 -0.40 ┤             ╰╮        │              ╰╮        ╭
 -0.60 ┤              ╰╮      ╭╯               ╰╮      ╭╯
 -0.80 ┤               ╰─╮  ╭─╯                 ╰─╮  ╭─╯
 -1.00 ┤                 ╰──╯                     ╰──╯
>>>> Chart for Cos(d/4)
  1.00 ┼─╮                     ╭──╮                     ╭
  0.80 ┤ ╰─╮                 ╭─╯  ╰─╮                  ╭╯
  0.60 ┤   ╰╮               ╭╯      ╰╮                ╭╯
  0.40 ┤    ╰╮              │        ╰╮              ╭╯
  0.20 ┤     │             ╭╯         ╰╮            ╭╯
  0.00 ┤     ╰╮           ╭╯           │           ╭╯
 -0.20 ┤      ╰╮         ╭╯            ╰╮         ╭╯
 -0.40 ┤       ╰╮       ╭╯              ╰╮       ╭╯
 -0.60 ┤        ╰╮     ╭╯                ╰╮     ╭╯
 -0.80 ┤         ╰╮   ╭╯                  ╰╮   ╭╯
 -1.00 ┤          ╰───╯                    ╰───╯
>>>> Chart for d*d
 2401.00 ┤                                               ╭─
 2160.90 ┤                                             ╭─╯
 1920.80 ┤                                          ╭──╯
 1680.70 ┤                                       ╭──╯
 1440.60 ┤                                    ╭──╯
 1200.50 ┤                                ╭───╯
  960.40 ┤                            ╭───╯
  720.30 ┤                        ╭───╯
  480.20 ┤                  ╭─────╯
  240.10 ┤          ╭───────╯
    0.00 ┼──────────╯

Links

About

Test loading and unloading assemblies in .NET 6 with AssemblyLoadContext

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages