none
Create Custom Hosting Service for console app on dnxcore50 RRS feed

  • Question

  • Hi,

    I have created a console app to send email on scheduled time. I am creating a windows service to do this. But windowsservices are not available in .netcore. Can I know the solution for this. 

    I am getting an error while inheriting ServiceBase, as there is no System.ServiceProcess in dnxcore50. I can't user net451 or other framework as my application uses .netCore in production so I have use only this dnxcore50. 

    Below is the code in my main method

      public static void Main(string[] args)
            {

    var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(contentRootPath)
            .Build();

                if (Debugger.IsAttached || args.Contains("--debug"))
                {
                    host.Run();
                }
                else
                {
                    host.RunAsCustomService();
                }

    }

    Below is my project.json file

         

    {
      "version": "1.0.0-*",

      "dependencies": {
        "Microsoft.AspNetCore.Http.Abstractions": "1.0.0",
        "NETStandard.Library": "1.6.0",
        "Newtonsoft.Json": "9.0.1",
        "Microsoft.Extensions.DependencyInjection": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0",
        "Microsoft.Extensions.Logging.Debug": "1.0.0",
        "Microsoft.Extensions.PlatformAbstractions": "1.0.0",
        "Bz.Business.Manager": "1.0.0-*",
        "Bz.Business.Models": "1.0.0-*",
        "Bz.Core.Repository": "1.0.0-*",
        "Serilog": "2.1.0",
        "Serilog.Extensions.Logging": "1.1.0",
        "Serilog.Sinks.File": "2.1.0",
        "Serilog.Sinks.Literate": "2.1.0-dev-00031",
        "Serilog.Sinks.RollingFile": "2.1.0",
        "Microsoft.Extensions.Configuration": "1.0.0",
        "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
        "Microsoft.Extensions.Configuration.Json": "1.0.0",
        "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
        "Microsoft.AspNetCore.Hosting": "1.0.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0",
        "Microsoft.Framework.Runtime.Interfaces": "1.0.0-beta4"
      },

      "frameworks": {
        "netstandard1.6": {
          "imports": "dnxcore50"

        }


      }
    }

    Below is the CustomWebHostService.cs

    using Microsoft.AspNetCore.Hosting;

    using System.ServiceProcess;
    using Microsoft.Extensions.DependencyInjection;

    namespace Bz.Utilities.AlertsProcessor
    {
        //    / <summary>
        //    /     Provides an implementation of a Windows service that hosts ASP.NET Core.
        //    / </summary>
        public class WebHostService : ServiceBase
        {
            private IWebHost _host;
            private bool _stopRequestedByWindows;

            /// <summary>
            /// Creates an instance of<c>WebHostService</c> which hosts the specified web application.
            /// </summary>
            /// <param name = "host" > The configured web host containing the web application to host in the Windows service.</param>
            public WebHostService(IWebHost host)
            {
                _host = 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();
                _host?.Dispose();
                OnStopped();
            }

            /// <summary>
            /// Executes before ASP.NET Core starts.
            /// </summary>
            /// <param name = "args" > The command line arguments passed to the service.</param>
            protected virtual void OnStarting(string[] args) { }

            /// <summary>
            /// Executes after ASP.NET Core starts.
            /// </summary>
            protected virtual void OnStarted() { }

            /// <summary>
            /// Executes before ASP.NET Core shuts down.
            /// </summary>
            protected virtual void OnStopping() { }

            /// <summary>
            /// Executes after ASP.NET Core shuts down.
            /// </summary>
            protected virtual void OnStopped() { }
        }


        internal class CustomWebHostService : WebHostService
        {
            public CustomWebHostService(IWebHost host) : base(host)
            {
            }

            protected override void OnStarting(string[] args)
            {
                // Log
                base.OnStarting(args);
            }

            protected override void OnStarted()
            {
                // More log
                base.OnStarted();
            }

            protected override void OnStopping()
            {
                // Even more log
                base.OnStopping();
            }
        }
        public static class CustomWebHostWindowsServiceExtensions
        {
            public static void RunAsCustomService(this IWebHost host)
            {
                var webHostService = new CustomWebHostService(host);
                ServiceBase.Run(webHostService);
            }
        }
    }

    Below is the error



    using Microsoft.Extensions.DependencyInjection;

    Tuesday, September 27, 2016 3:20 PM

All replies

  • Hi Ramya gollapudi,

    Thank you for posting here.

    According to your code. I think your issue is related to ASP.NET forum.

    Could you tell us what type of your project? it is a ASP.NET webservices? if yes, I would suggest you repost

    here.

    Based on my search. I find a similar solution.

    You've got a couple of options here - use Microsoft's WebHostService class, inherit WebHostService or write your own.

    I hope the information is helpful to you.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, September 28, 2016 2:32 AM
  • Hi Hart,

    My project is Console Application(.Net Core). I am trying to create windows service for this project.

    Thanks,

    Ramya.

    Thursday, September 29, 2016 8:57 AM
  • Hi Ramya,

    >>My project is Console Application(.Net Core). I am trying to create windows service for this project.

    What's the problem now? Do you have any updates until now? 

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 18, 2016 3:05 AM