服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - .NET Core 3.0 可回收程序集加载上下文的实现

.NET Core 3.0 可回收程序集加载上下文的实现

2020-06-15 14:18YOYOFx ASP.NET教程

这篇文章主要介绍了.NET Core 3.0 可回收程序集加载上下文的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、前世今生

.NET诞生以来,程序集的动态加载和卸载都是一个Hack的技术,之前的NetFx都是使用AppDomain的方式去加载程序集,然而AppDomain并没有提供直接卸载一个程序集的API,而是要卸载整个AppDomain才能卸载包含在其中的所有程序集。然而卸载整个CurrentAppDomain会使程序不能工作。可能有人另辟西经,创建别一个AppDomain来加载/卸载程序集,但是由于程序集之间是不能跨域访问的,也导致只能通过Remote Proxy的方式去访问,这样在类型创建和使用上带来了一定的难度也是类型的继承变得相当复杂。

.NET Core中一直没有AppDomain的支持。但是在.NET Core 3.0中,我最期待的一个特性就是对可收集程序集的支持(Collectible AssemblyLoadContext)。 众所周知.NET Core中一直使用AssemblyLoadContext的API,来进行程序集的动态加载,但是并没有提供Unload的方法,此次升级更新了这方面的能力。

二、AssemblyLoadContext

其实这次AssemblyLoadContext的设计,我认为更像是Java中ClassLoader的翻版,可以说非常类似。在使用过程中自定义AssemblyLoadContext可以内部管理其中的程序集,并对整体Context进行Unload。使用AssemblyLoadContext也可以避免程序集名称和版本的冲突。

三、Getting Started

.NET Core 3.0还没有正式版,所有要使用预览版的SDK完成以下实例。我使用的是.NET Core SDK 3.0.100-preview-009812

?
1
dotnet new globaljson --sdk-version 3.0.100-preview-009812

AssemblyLoadContext是一个抽象类的,我们需要子类化。下面显示的是我们创建自定义AssemblyLoadContext的方法,实现一个可回收的Context需要在构造器中指定isCollectible: true :

?
1
2
3
4
5
6
7
8
9
10
public class CollectibleAssemblyLoadContext : AssemblyLoadContext
{
  public CollectibleAssemblyLoadContext() : base(isCollectible: true)
  { }
 
  protected override Assembly Load(AssemblyName assemblyName)
  {
    return null;
  }
}

使用netstandard2.0创建一个library

?
1
2
3
4
5
6
7
8
9
10
11
12
using System;
 
namespace SampleLibrary
{
  public class SayHello
  {
    public void Hello(int iteration)
    {
      Console.WriteLine($"Hello {iteration}!");
    }
  }
}

测试Load/Unload

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var context = new CollectibleAssemblyLoadContext();
var assemblyPath = Path.Combine(Directory.GetCurrentDirectory(),"SampleLibrary.dll");
using (var fs = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read))
{
  var assembly = context.LoadFromStream(fs);
 
  var type = assembly.GetType("SampleLibrary.SayHello");
  var greetMethod = type.GetMethod("Hello");
 
  var instance = Activator.CreateInstance(type);
  greetMethod.Invoke(instance, new object[] { i });
}
 
context.Unload();
 
GC.Collect();
GC.WaitForPendingFinalizers();

当执行GC收回后,加载的程序集会被完全的回收。

四、最后

GitHub:https://github.com/maxzhang1985/YOYOFx 如果觉还可以请Star下, 欢迎一起交流。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/maxzhang1985/p/10875278.html

延伸 · 阅读

精彩推荐