In the last article, there are five ways to deploy ASP.NET Core website. One way is to deploy it through Windows service, so that you can start self-starting without reversed by iis. Agents lose some performance.
But the fly in the ointment is that it needs to be done with the third-party software nssm. Is there a better and elegant way to deploy the Windows service mode of the ASP.NET Core website?
The answer is yes!

  1. Create a new asp.net core website project, as shown in the figure below, I named it TestAgentHow-to-elegantly-use-the-Windows-service-to-depl1
  2. Need to add two Nuget packages as shown below, this is the famous NewLife.Core and NewLife.Agent
    Install-Package NewLife.Core
    Install-Package NewLife.Agent

    After installation, it looks like this:

    How-to-elegantly-use-the-Windows-service-to-depl2

  3. At this point we slightly modify the Program to add a service class and inherit from AgentServiceBase, as shown below:

    Namespace TestAgent
    {
    Public class Program
    {
    Protected static string[] _args;
    Public static void Main(string[] args)
    {
    _args = args;
    TestAgentServices.ServiceMain();

    }

    Public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .UseUrls(“http://*:8008”)
    .UseStartup<Startup>();

    Public class TestAgentServices : AgentServiceBase<TestAgentServices>
    {
    #region attribute

    /// <summary>display name</summary>
    Public override string DisplayName => “Agent Test Service”;

    /// <summary>description</summary>
    Public override string Description => “Description of the agent test service!”;
    #endregion

    #region constructor
    /// <summary> instantiate a proxy service</summary>
    Public TestAgentServices()
    {
    / / Generally specify the service name in the constructor
    ServiceName = “TestAgent”;
    }
    #endregion

    #region Execution task
    Protected override void StartWork(string reason)
    {

    CreateWebHostBuilder(_args).Build().Run();
    WriteLog(“current time {0}”, DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss”));
    base.StartWork(reason);
    }
    #endregion
    }
    }
    }

    As shown above: we rewrite the name and description information displayed and

    CreateWebHostBuilder(_args).Build().Run();

    move it to the

    StartWork

    method of this service.

  4. Called

    TestAgentServices.ServiceMain();

    in the Mian method to boot into the new service class we created above.
  5. Open PowerShell as an administrator and

    dotnet run

    run our program as shown in the following figure, and see the following interface:How-to-elegantly-use-the-Windows-service-to-depl3

    As shown above: the name, description and status of the current service are displayed. The yellow font section lists the corresponding operation commands. We only need to input the corresponding data to operate.

  6. We enter the numbers directly here

    2

    to install the service as follows:How-to-elegantly-use-the-Windows-service-to-depl4

    At this time the display service has been installed, but has not yet started, at this time we can open the windows service window to find us Agent test service, and the right-click properties are as follows:

    How-to-elegantly-use-the-Windows-service-to-depl5

    At this time, you can start it!

  7. We visit the port we configured to access the next website as follows:How-to-elegantly-use-the-Windows-service-to-depl6

    At this point, our windows service mode is deployed successfully!

  8. Finally, it is explained that NewLife.Agent is the netcore version of XAgent, and the nfx version is integrated in NewLife.Core. That is to say, nfx only needs to reference NewLife.Core, which is better for Windows compatibility.NewLife.Agent is independent in preparation for supporting Linux

Original link:

https://www.cnblogs.com/yilezhu/p/10380887.html