This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2833|Reply: 1

[.NET Core] .NET/C# implements DLL plugin hot updates based on AssemblyLoadContext

[Copy link]
Posted on 2024-6-28 14:28:33 | | | |
Requirements: The business logic is complex and requires customization, and for such logic, in a separate DLL assembly, the background can dynamically update the assembly and execute the latest logic.

.NET Core doesn't support dynamic creation and unloading of AppDomains like the .NET Framework, so there has never been a good way to hotload plugins, but the good news is that .NET Core has supported Collectible Assembly since 3.0, and we can create a reclaimable AssemblyLoadContext that we can use to load and unload assemblies.

AssemblyLoadContext class:The hyperlink login is visible.

First, create a new console and three libraries, namely:ConsoleApp6ClassLibraryStandardClassLibrary1ClassLibrary2, among which the ClassLibrary1 and ClassLibrary2 libraries inherit the IWorker interface in ClassLibraryStandard and implement the Run method.

ClassLibraryStandard code:

ClassLibrary1 code:

ClassLibrary2 code:

The ConsoleApp6 console code is as follows:

The console opens the Run method of 3 threads in the DLL plugin that is constantly dynamically executing, and uses log4net to record the output results, and then runs it in the consoleReplace the DLL with the latest library after 3 seconds

After running the console for 1 second or 5 seconds, the collection of the current AssemblyLoadContext instance is output, which is the loaded DLL library.

As shown below:



You can see that the program does not have any abnormal output, and there is no situation where the plugin cannot be found during the execution process. The reason why log4net records the execution results is because the console is blocked, and in order to ensure that the 3 threads can execute continuously, log4net is used to record the execution results, as shown in the figure below:



2024-06-28 14:24:36,606 INFO 11 RollingFile - 1
2024-06-28 14:24:36,606 INFO 10 RollingFile - Start uninstalling
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
2024-06-28 14:24:36,606 INFO 11 RollingFile - 1
2024-06-28 14:24:36,606 INFO 11 RollingFile - 1
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
2024-06-28 14:24:36,606 INFO 10 RollingFile - AssemblyLoadContext_Unloading
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
2024-06-28 14:24:36,606 INFO 13 RollingFile - 1
...
2024-06-28 14:24:36,607 INFO 12 RollingFile - 1
2024-06-28 14:24:36,607 INFO 13 RollingFile - 1
2024-06-28 14:24:36,607 INFO 12 RollingFile - 1
2024-06-28 14:24:36,606 INFO 10 RollingFile - Uninstall complete
2024-06-28 14:24:36,607 INFO 12 RollingFile - 1
2024-06-28 14:24:36,607 INFO 12 RollingFile - 1
...
2024-06-28 14:24:36,607 INFO 12 RollingFile - 1
2024-06-28 14:24:36,607 INFO 13 RollingFile - 1
2024-06-28 14:24:36,607 INFO 12 RollingFile - 1
2024-06-28 14:24:36,607 INFO 13 RollingFile - 1
2024-06-28 14:24:36,607 INFO 12 RollingFile - 1
2024-06-28 14:24:36,606 INFO 11 RollingFile - 1
2024-06-28 14:24:36,607 INFO 13 RollingFile - 2
2024-06-28 14:24:36,607 INFO 12 RollingFile - 2

Attached config.xml configuration:

(End)




Previous:About QRCODE QR code fault tolerance level
Next:.NET/C# uses PDFsharp to merge multiple PDF files into one
 Landlord| Posted on 2024-7-6 16:59:30 |
Use Roslyn to dynamically compile C# code and run it
https://www.itsvse.com/thread-10786-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com