C# .NET Core 3.1 中 AssemblyLoadContext 的基本使用

前言

之前使用 AppDomain 写过一个动态加载和释放程序的案例,基本实现了自己“兔死狗烹”,不留痕迹的设想。无奈在最新的 .NET Core 3.1 中,已经不支持创建新的 AppDomain 了(据说是因为跨平台实现太重了),改为使用 AssemblyLoadContext 了。不过总体使用下来感觉比原来的 AppDomain 要直观。

不过这一路查找资料,感觉 .NET Core 发展到 3.1 的过程还是经历了不少的。比如 2.2 的 API 与 3.1 就不一样(自己的体会,换了个版本就提示函数参数错误), preview版中 AssemblyLoadContext 卸载后无法删除库文件,但是版本升级后就好了(github 上的一篇讨论)

本文主要是关于 AssemblyLoadContext 的基本使用,加载和释放类库。

基本使用

程序的基本功能是:动态加载 Magick 的所需库,并调用其压缩图片的函数压缩给定图片。(歪个楼,Magick 和 Android 的 Magisk 这两个看起来太像了)

using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader; namespace AssemblyLoadContextTest
{
class Program
{
static void Main(string[] args)
{
WeakReference weakReference; Compress(out weakReference); for (int i = 0; weakReference.IsAlive && (i < 10); i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
} Console.WriteLine($"卸载成功: {!weakReference.IsAlive}");
} [MethodImpl(MethodImplOptions.NoInlining)]
public static void Compress(out WeakReference weakReference)
{
AssemblyLoadContext alc = new AssemblyLoadContext("CompressLibrary", true); // 新建一个 AssemblyLoadContext 对象 weakReference = new WeakReference(alc); Assembly assembly0 = alc.LoadFromAssemblyPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Magick.NET.Core.dll"));
Assembly assembly1 = alc.LoadFromAssemblyPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Magick.NET-Q16-AnyCPU.dll")); string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image_to_compress.jpg"); Console.WriteLine("压缩前大小:" + new FileInfo(filePath).Length); var magickImageType = assembly1.GetType("ImageMagick.MagickImage"); // 已知该类定义在 assembly1 中
var magickImageIns = Activator.CreateInstance(magickImageType, new object[] { filePath }); // magickImageIns = new ImageMagick.MagickImage(filePath)
var qualityProperty = magickImageType.GetProperty("Quality");
qualityProperty.SetValue(magickImageIns, 60); // magickImageIns.Quality = 60
var writeMethod = magickImageType.GetMethod("Write", new Type[] { typeof(string) });
writeMethod.Invoke(magickImageIns, new object[] { filePath }); // magickImageIns.Write(filePath) Console.WriteLine("压缩后大小:" + new FileInfo(filePath).Length); var disposeMethod = magickImageType.GetMethod("Dispose");
disposeMethod.Invoke(magickImageIns, null); // magickImageIns.Dispose() //magickImageIns = null;
alc.Unload();
}
}
}

加载不用多说,创建实例加载即可;卸载时需要注意的是一下几点:

  1. 使用 AssemblyLoaderContext 加载和卸载的代码必须要单独放在一个方法,不可以写在 Main 方法中,否则加载的模块只有等待整个程序退出后才能卸载
  2. 方法中应加上 [MethodImpl(MethodImplOptions.NoInlining)] 特性,否则可能也不会正常卸载(在本例子中似乎不加也可以),官方示例是这么说的:

It is important to mark this method as NoInlining, otherwise the JIT could decide

to inline it into the Main method. That could then prevent successful unloading

of the plugin because some of the MethodInfo / Type / Plugin.Interface / HostAssemblyLoadContext

instances may get lifetime extended beyond the point when the plugin is expected to be

unloaded.

  1. 卸载的过程是异步的,调用了以后并不会立刻完成
  2. 如果一定要等待其完成可以通过创建一个 WeakReference 指向它,通过查看 WeakReference 是否存在来判断是否完成释放。 但等待释放的方法要在“加载卸载的代码”方法外,否则依然无法查看到它被回收
  3. 还有一点比较奇怪,如果我在最后不加 magickImageIns = null; 这一句,有时可以卸载,有时又无法卸载。如果类似的情况无法卸载,可以加上试试。

TIPS

在 Visual Studio 中提供了“模块窗口”,可以及时查看加载了哪些程序集,在 “调试” > “窗口” > “模块”

简单对比 AppDomain

AppDomain 似乎是一个大而全的概念,包括了程序运行的方方面面:工作路径、引用搜索路径、配置文件、卷影复制 等,而 AssemblyLoadContext 只是一个加载程序集的工具。

参考

官方示例(参看其中的 /Host/Program.cs)

Visual Studio 中的 模块 窗口

https://docs.microsoft.com/zh-cn/visualstudio/debugger/how-to-use-the-modules-window?view=vs-2019

这篇挺详细的,很多问题我没有深入地研究,但是其中的“需要的变量放到静态字典中.在Unload之前把对应的Key值删除掉”我不认同,也可能是因为版本原因吧

https://www.cnblogs.com/LucasDot/p/13956384.html

提问者无意间通过 ref 引用了 AssemblyLoadContext 对象而导致无法回收

https://stackoverflow.com/questions/55693269/assemblyloadcontext-did-not-unload-correctly

最后的测试方法应该单独写在一个方法中而不是在 Main 函数中(作者没有显式指明,我在这困扰了好久)

https://www.cnblogs.com/maxzhang1985/p/10875278.html

C# .NET Core 3.1 中 AssemblyLoadContext 的基本使用的更多相关文章

  1. 使用 .NET Core 3.0 的 AssemblyLoadContext 实现插件热加载

    一般情况下,一个 .NET 程序集加载到程序中以后,它的类型信息以及原生代码等数据会一直保留在内存中,.NET 运行时无法回收它们,如果我们要实现插件热加载 (例如 Razor 或 Aspx 模版的热 ...

  2. .net core 3.0中动态卸载程序集

    动态加载程序集在一些插件式的应用中非常常见,.net core 2.0中可以通过AssemblyLoadContext中提供程序集的动态加载功能,但取不支持卸载.现在,在.net core 3.0中提 ...

  3. ASP.NET Core HTTP 管道中的那些事儿

    前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Core 中间件系列的,但是中间遇到了很多事情.首先是 NPOI ...

  4. 在.NET Core控制台程序中使用依赖注入

    之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...

  5. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  6. 在ASP.NET Core 1.0中如何发送邮件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...

  7. EF Core 1.0中使用Include的小技巧

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于EF Core暂时不支持Lazy Loading,所以利用Include来加载额外 ...

  8. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  9. 用ASP.NET Core 1.0中实现邮件发送功能

    准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...

随机推荐

  1. 第3篇-CallStub新栈帧的创建

    在前一篇文章 第2篇-JVM虚拟机这样来调用Java主类的main()方法  中我们介绍了在call_helper()函数中通过函数指针的方式调用了一个函数,如下: StubRoutines::cal ...

  2. 天梯赛 L1-058 6翻了

    传送门:https://pintia.cn/problem-sets/994805046380707840/problems/1111914599408664577 这道字符串题,只是天梯赛L1的题, ...

  3. Notes of Cygwin in Windows7

    Installation download setup.exe from its official website; run setup.exe, select "download with ...

  4. MyBatis学习05(多对一和一对多)

    8.多对一的处理 多对一的理解: 多个学生对应一个老师 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师! 数据库设计 CREATE TABLE `teacher` ( `id` IN ...

  5. Redis-03-集群

    集群介绍 Redis Cluster 是 redis 的分布式解决方案, 在3.0版本正式推出,当遇到单机.内存.并发.流量等瓶颈时,可以采用Cluster架构方案达到负载均衡目的 Redis Clu ...

  6. sqli-labs lesson 46-53

    写在前面: 关于 order by 例: select * from users order by 1 ;   将users表中的第1列按照从小到大依次排列 select * from users o ...

  7. sqli-labs lesson 32-37

    宽字节注入: 原理:mysql在使用GBK编码的时候,会认为两个字符为一个汉字,例如%aa%5c就是一个汉字(前一个ascii码大于128才能到汉字的范围).我们在过滤 ' 的时候(也就是从防御的角度 ...

  8. ITIL学习笔记——ITIL入门小知识

    1. 什么是ITIL? ITIL即IT基础架构库(Information Technology Infrastructure Library)由英国政府部门CCTA(Central Computing ...

  9. 在WPF中的ItemsControl中使用事件和命令(Using events and Commands within ItemsControl in WPF)

    Say I have a standard WPF ItemsControl bound to an ObservableCollection of "Dog" objects l ...

  10. SpringCloud之Config

    1.背景 在前的学习中,我们几乎解决了springCloud的所有常规应用,但是大家有没有想过这样一个问题: 是使用微服务后,有非常多的application.yml文件,每个模块都有一个,实际开发中 ...