NetCore 2.1 Generic Host as a service

前端 未结 3 1073
北恋
北恋 2021-02-08 18:07

I\'m trying to build a Windows Service using the latest Dotnet Core 2.1 runtime. I\'m NOT hosting any aspnet, I do not want or need it to respond to http requests.

I\

相关标签:
3条回答
  • 2021-02-08 18:47

    I hope you found the solution for this problem.

    In my case I used generic host (introduced in 2.1) for such purpose and then just wrap it up with systemd to run it as a service on Linux host.

    I wrote a small article about it https://dejanstojanovic.net/aspnet/2018/june/clean-service-stop-on-linux-with-net-core-21/

    I hope this helps

    0 讨论(0)
  • 2021-02-08 19:00

    As others have said you simply need to reuse the code that is there for the IWebHost interface here is an example.

    public class GenericServiceHost : ServiceBase
    {
        private IHost _host;
        private bool _stopRequestedByWindows;
    
        public GenericServiceHost(IHost host)
        {
            _host = host ?? throw new ArgumentNullException(nameof(host));
        }
    
        protected sealed override void OnStart(string[] args)
        {
            OnStarting(args);
    
            _host
                .Services
                .GetRequiredService<IApplicationLifetime>()
                .ApplicationStopped
                .Register(() =>
                {
                    if (!_stopRequestedByWindows)
                    {
                        Stop();
                    }
                });
    
            _host.Start();
    
            OnStarted();
        }
    
        protected sealed override void OnStop()
        {
            _stopRequestedByWindows = true;
            OnStopping();
            try
            {
                _host.StopAsync().GetAwaiter().GetResult();
            }
            finally
            {
                _host.Dispose();
                OnStopped();
            }
        }
    
        protected virtual void OnStarting(string[] args) { }
    
        protected virtual void OnStarted() { }
    
        protected virtual void OnStopping() { }
    
        protected virtual void OnStopped() { }
    }
    
    public static class GenericHostWindowsServiceExtensions
    {
        public static void RunAsService(this IHost host)
        {
            var hostService = new GenericServiceHost(host);
            ServiceBase.Run(hostService);
        }
    }
    
    0 讨论(0)
  • 2021-02-08 19:09

    IHostedService if for [asp.net core] backendjob, if u want to build a windows service on .net core, u should reference this package System.ServiceProcess.ServiceController, and use ServiceBase as base class. (you can also start from a .net framework windows service and then change the .csproj file)


    edit: please see this doc and this code https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting.WindowsServices/WebHostWindowsServiceExtensions.cs. To create a windows service ServiceBase for manage your IHost

    0 讨论(0)
提交回复
热议问题