Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running .net core application as windows service #1845

Closed
rpundlik opened this issue Nov 24, 2016 · 5 comments
Closed

Running .net core application as windows service #1845

rpundlik opened this issue Nov 24, 2016 · 5 comments

Comments

@rpundlik
Copy link

rpundlik commented Nov 24, 2016

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 !

@guardrex
Copy link
Contributor

guardrex commented Nov 24, 2016

I don't think ServiceBase has been ported to Core yet. See: https://github.com/dotnet/corefx/issues/6024

[EDIT] Also, see: #1386 and especially https://github.com/dasMulli/dotnet-win32-service

@PeterKottas
Copy link

Hi there, I think I have exactly what you are looking for. Check this out, hope it helps : https://github.com/PeterKottas/DotNetCore.WindowsService

@h3smith
Copy link

h3smith commented Feb 21, 2017

I know this won't help everyone, but the easiest solution for us was to drop a CLI application in Docker and tell Docker to restart it if it failed.

@PeterKottas
Copy link

Yep for docker that's probably the best solution.

@aspnet-hello
Copy link

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

ryanbrandenburg pushed a commit that referenced this issue Nov 27, 2018
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants