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:ConsoleApp6、ClassLibraryStandard、ClassLibrary1、ClassLibrary2, 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)
|