C # implement the specific process of hot update server program

Time:2021-9-27
catalogue
  • Does hot update solve some development problems
  • Implementation principle of hot update
  • Specific implementation process
  • summary

Does hot update solve some development problems

In fact, the first question to be discussed is whether it is necessary for hotter server programs to appear, or whether there are applicable scenarios. Personally, I think there are still some.

If you can really update the logic without restarting the application, this is not good news for some emergencies. Especially when some small teams and small start-ups are just starting, the technical testing process and manpower will not be as perfect as those of large-scale enterprises. The problem is that there may be some bugs in the applications and games after they are launched, If it’s not serious, it’s OK to leave it for the next iteration to update and repair uniformly. However, if it’s serious, it’s considered that closing the service repair at the moment will lead to the loss of users. At this time, there will be an application scenario for non-stop hot update, which can carry out logical repair without the user’s perception. This is also the main application scenario of the hotter function. Adding new functions or deleting some functions can also be operated by non-stop service.

After finishing this requirement, the next step is how to realize this function. In fact, this function mainly uses c# built-in reflection. After. Net core 3.0 and. Net5.0 in the new dotnet framework, Microsoft has further sorted out assembly loading. Although there were similar functions in the past, it is well known that the previous. Net version was chaotic. Now Microsoft is both open-source and unified. It can be said that in the future, The potential of. Net is immeasurable, so what I’m talking about is also based on a newer Framework version.

Implementation principle of hot update

The assemblyloadcontext class is today’s protagonist. Its main function is isolated program context. What does it mean? It has certain protection, so that dynamically loaded assemblies can not be mixed with statically loaded assemblies, but run independently in a sandbox like space, but have mutual access permissions. This is very powerful. In this way, we can load our own assembly for the original application to call. When it needs to be updated, we can load the new assembly, replace the old assembly and release the old assembly, so that we can seamlessly execute the new logic.

Next, I will borrow the framework I wrote to realize this process of hotter. If you are interested in the hotter principle itself, you can also see the hotter source code I wrote. I will post my open source project address at the bottom of the article. The source code is basically annotated, so it won’t be difficult.

Specific implementation process

First, we create a. Net5.0 or. NETCORE 3.1 project named ABC. ​

After the creation is successful, we find the item, right-click the manage nuget package, select the browse tab search: easysharpframe, and then select it and click the download button to install it.

After the installation is completed, the framework is successfully installed. Next, we will right-click the solution to create a new project. This project is a dynamic library for hot logic implementation, called hotfix. After creation, we will see that this project has been successfully added in the column of Solution Explorer, After that, we right-click the dependency of the hotfix project, find the add project reference, and directly check ABC to confirm. So far, the add and create steps of the project are completed. ​​​​

Next is to start writing code for practical application. First, in the Program.cs entrance of Abc main project, the hotbox is called to manage the single instance more. Because we have not changed the default configuration of hotfix project, the generated DLL file name is hotfix. Then we add an entry file of main.cs in hotfix project. Here, the initialization is completed through the hotfix manager. For each overload, we only need to repeat this step.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        //Ensure that the process does not end automatically
        while (true)
        {
            //Call the hotfix module to load hotfix
            HotfixMgr.Instance.Load("Hotfix", "Hotfix.Main");
            //Enter to continue, otherwise it will be blocked
            Console.ReadLine();
        }
    }
}

Next, we add some test content to main.cs in hotfix project.


public class Main
{
    public void Hello()
    {
        Console.WriteLine("Hello World");
    }
}

And just add something to the main project code.

//Call the hotfix module to load hotfix
HotfixMgr.Instance.Load("Hotfix", "Hotfix.Main");
//Here, you can call the function of the test entry through the agent
HotfixMgr.Instance.Agent.Hello();
//Enter to continue, otherwise it will be blocked
Console.ReadLine();

Such a simple example is completed, and then the solution is generated. Then find the hotfix project generation path, copy and paste the hotfix.dll and hotfix.pdb files into the generation path of ABC. Double click abc.exe to start the case and view the results. Note: PDB is used to provide the runtime with an explanation file that accurately prompts errors when errors occur. In actual operation, only DLL is required. If you do not need to check the source of the problem, you can only copy DLL. ​

At this time, do not close the console, continue to return to VS, modify the contents of main.cs, and only generate hotfix project this time, and copy the generated DLL and PDB to ABC.

public class Main
{
    public void Hello()
    {
        //Before modification
        // Console.WriteLine("Hello World");
        //After modification
        Console.WriteLine("Hello New World");
    }
}

After the replacement, press enter on the console, and the result shows that the new logic has been updated to the program.

The above is a simple demonstration of the heat change process. The heat change function provided by the framework has more functions. Here is a brief introduction to the principle and implementation of heat change. If you are interested in the framework, you can go to GitHub to learn about it.

Project address: github.com/suxf/easysh

summary

This is the end of this article about c# implementation of hot update server program. For more information about c# hot update server program, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!