Skip to content

Running .net core application as windows service #1845

Closed
@rpundlik

Description

@rpundlik

Hello All,
I am trying to create and run the .net core application as windows service. While creating the service I am using WebHostService class where OnStarted,OnStarting etc such method are overridable.
So my question is in which method I should write my business logic(like getting data from db ett.) Started or Starting ?
Because previously we used to write in OnStart method but that is not available(it is sealed method)
Or do I need to use ServiceBase class to use OnStart method ?
Below is my class

public class SendMailHostService : WebHostService
    {
        private readonly EventLog _log = new EventLog("Application") { Source = "Application" };
        public SendMailHostService(IWebHost host) : base(host)
        {

        }
        protected override void OnStarted()
        {
//#if DEBUG
//            Thread.Sleep(30000);
//#endif
            Console.WriteLine("Asp.net service started.");
            Console.ReadLine();
            base.OnStarted();
        }
        protected override void OnStarting(string[] args)
        {
            //#if DEBUG
            //            Thread.Sleep(30000);
            //#endif

            Console.WriteLine("Asp.net service starting.");
            Console.ReadLine();
            base.OnStarting(args);
        }
}

Also when I run the service using net start command nothing is printed on the command prompt ? why is it so ?

Thanks for the help !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions