Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the loading context of .NET Core 3.0 recyclable assemblies

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to implement the .NET Core 3.0recyclable assembly loading context". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to implement the .NET Core 3.0recyclable assembly loading context.

I. past life and present life

Since the birth of. Net, the dynamic loading and unloading of assemblies is a Hack technology. The previous NetFx uses AppDomain to load assemblies. However, AppDomain does not provide an API to unload an assembly directly, but unloads the entire AppDomain to unload all assemblies contained in it. However, uninstalling the entire CurrentAppDomain will prevent the program from working. Someone may create another AppDomain to load / unload assemblies, but because assemblies cannot be accessed across domains, it can only be accessed through Remote Proxy, which brings some difficulty in type creation and use and makes type inheritance quite complex.

There has been no support for AppDomain in the .NET Core. But one of the features I most look forward to in .NET Core 3.0is Collectible AssemblyLoadContext support for collectable assemblies. It is well known that AssemblyLoadContext's API has been used in .NET Core for dynamic loading of assemblies, but no Unload method is provided, and this upgrade updates this capability.

II. AssemblyLoadContext

In fact, the design of AssemblyLoadContext, I think, is more like a copy of ClassLoader in Java, which can be said to be very similar. During the use of a custom AssemblyLoadContext, you can manage the assemblies internally and Unload the overall Context. Using AssemblyLoadContext can also avoid assembly name and version conflicts.

III. Getting Started

There is no official version of .NET Core 3.0, so you need to use the preview version of SDK to complete the following examples. I am using the .NET Core SDK 3.0.100-preview-009812

Dotnet new globaljson-sdk-version 3.0.100-preview-009812

AssemblyLoadContext is an abstract class, and we need subclassing. The following shows how we create a custom AssemblyLoadContext. To implement a recyclable Context, you need to specify isCollectible: true in the constructor:

Public class CollectibleAssemblyLoadContext: AssemblyLoadContext {public CollectibleAssemblyLoadContext (): base (isCollectible: true) {} protected override AssemblyLoad (AssemblyName assemblyName) {return null;}} create a libraryusing System; namespace SampleLibrary {public class SayHello {public void Hello (int iteration) {Console.WriteLine ($"Hello {iteration}!") using netstandard2.0 } Test Load/Unload

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 ()

When the GC is reclaimed, the loaded assembly is completely recycled.

Thank you for reading, the above is the ".NET Core 3.0recyclable assembly loading context how to implement" content, after the study of this article, I believe you have a deeper understanding of the .NET Core 3.0recyclable assembly loading context how to achieve this problem, the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report