I have long wanted to write an article about hot more server, so I will write it these two days. It happened that I also spent some time to clean up the C# framework I had written for some time in the past two days.

Hot updates do not address the pain points of some development issues

In fact, one of the first questions to discuss is whether hot change server program is necessary, or whether there is a applicable scenario, I personally think there is.

If it can be realized in the case of don’t restart the application update logic, it may in the case of some emergency is not a good news, especially some small teams, small startup has just started, technical testing process and the human will not as perfect as into the scale of the enterprise, so the problem is that online applications, games, there might be some bugs, If it is not serious, it can be left to the next iteration for unified repair. However, if it is serious, if the repair will cause user loss at this moment, then there is an application scenario for continuous hot update, and the logical repair can be carried out without the user’s awareness. This is also the main application scenario of hot more features, as for adding new features or deleting some features can also be operated through non-stop.

So after saying this requirement, the next is how to achieve this function. In the new Dotnet framework, after.net core3.0 and.net5.0, Microsoft has made further improvements to Assembly loading. Although similar features have been available in the past, the well-known reason is that the previous.net versions were chaotic. Now that Microsoft is both open source and unified, it is fair to say that the potential of.NET in the future is immeasurable, so what I say is based on the newer version of the framework.

Hot update implementation principle

AssemblyLoadContext this class is to today’s leading role, its main function is the isolation of application context, what do you mean, is that it has certain protection, can make the dynamically loaded assembly with static loading assemblies are mixed together, but run independently in similar sandbox space, but also have the mutual access permissions. This is great because we can load our own assembly, then call it for the original application, and when we need to update, load the new assembly in, replace the old one, and release the old one, so that we can execute the new logic seamlessly.

Below I will borrow my own framework to achieve the process of hot more, if the principle of hot more itself interested, you can also go to see the hot more source CODE I wrote, I will paste my open source project address at the bottom of the article, the source code is basically annotated, so it will not look very difficult.

Specific implementation process

  • So let’s create one first. Net5.0 or. Netcore3.1 project, named Abc.

  • Once created, we found the “manage Nuget” item, right click on the “Manage Nuget” package, select the “browse” TAB to search: EasySharpFrame, then select the “download” button to install.

  • Framework after the installation is complete, if installed successfully, then, we again from solution right click on the create a new project, the project is used to heat more logic to realize dynamic library, is called the Hotfix, created, in the column of solution explorer will see that this project has been successfully added, Then right click on the dependency of the Hotfix project, go to the Add Project reference, check THE Abc check box, and the add project creation step is complete.

  • The next step is to start writing code for practical use, first in the Program. Cs entry in the Abc main project, calling the framework for hot management singletons. Since we did not change the default configuration of the Hotfix project, the generated Dll is named Hotfix. We then add a main.cs entry file to the Hotfix project, which is initialized by the Hotfix manager. We only need to repeat this step for each reload.

    class Program { static void Main(string[] args) { Console.WriteLine("Hello World!" ); While (true) {// Call Hotfix hotfixMgr.instance.load ("Hotfix", "hotfix.main "); // Press enter to continue, otherwise block console.readline (); }}}Copy the code
  • Next, let’s add some tests to the Hotfix project main.cs.

    public class Main { public void Hello() { Console.WriteLine("Hello World"); }}Copy the code
  • And add something to the main project code just now.

    Load("Hotfix", "hotfix.main "); // Call the Hotfix module to Load Hotfix hotfixMgr.instance-load ("Hotfix"," hotfix.main "); / / here you can test the entrance to the function called by the Agent HotfixMgr. Instance. Agent. Hello (); // Press enter to continue, otherwise block console.readline ();Copy the code
  • This is a simple example of heating, and the solution is generated. Then find the build path of the Hotfix project and copy and paste the two files about hotfix. DLL and hotfix. PDB into the build path of Abc. Double-click abc.exe to start the story and view the result. Note: The PDB is used to provide the runtime with an accurate error explanation file, the actual run only needs the DLL, if you do not need to see the source of the problem, you can just copy the DLL.

  • Do not close the console at this point, go back to VS, modify the contents of main.cs, and this time only generate the Hotfix project, copy the resulting DLL and PDB to Abc.

    Public class Main {public void Hello() {// before modification // console. WriteLine("Hello World"); // After the modification console. WriteLine("Hello New World"); }}Copy the code
  • When the replacement is complete, press enter on the console and the result shows that the new logic has been updated into the program.

This is a simple demonstration of thermo process, thermo function and more functions provided by the framework, here is just a brief introduction to the principle and implementation of thermo. If you are interested in the framework, you can go to Github.

Project Address:Github.com/suxf/EasySh…

If you feel comfortable, you can support the Github project with a star. Thank you very much