running a console .net core app as a windows service. easiest approach?

I have a console app that does some web requests to populate a database of cryptocurrency prices, the service needs to run twice a day.

Most of the tutorials i see online for running .net core windows service are those that also host a website e.g. https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.1

I had a look on the weekend for alternatives. it seems to boil down to these options, quoted from a stackoverflow post https://stackoverflow.com/questions/50477317/net-core-2-0-console-app-as-a-windows-service#_=_

  1. Use the Windows Compatilbitay Pack which brings back ServiceBase and related APIs to .NET Core, so TopShelf may work out of the box. (the pack is in RC at the time of writing with scheduled stable release about a week away)

  2. https://github.com/PeterKottas/DotNetCore.WindowsService seems to work for a lot of users so I suggest giving it a try.

  3. The library in (2) is a nice API around DasMulli.Win32.ServiceUtils which I wrote for our company to be able to deploy self-contained .NET Core Applications in production. So far we haven’t had any problems.

  4. Other service hosts which abstract the windows service infrastructure and run arbitrary programs – such as the Non-Sucking Service Manager. Note that this may not give good ways to gracefully shut down in reaction to a Stop command.

I had a stab at no1, seems the easiest, i suppose i can just forget about the web stuff and just use the servicecollection to register my dependencies. yet to try it out, ran out of time, but i have a feeling its going to bite me.

I also had a go at no2, though its a bit complicated compared to old school windows services.

What i noticed with no 1, is that there is no timer to set the elapsed call back function where your logic will be executed e.g. every 12 hours if i want to run it twice a day. The start() method was all that was shown in no1 example. in regular .net if this operation takes too long to run, the service will not work. sure i could wrap everything in a task and execute it, but that seems like a pretty crappy solution.

Does anyone have a better suggestion? Or am i missunderstanding how .net core windows services work compared to “the old .net windows service project item”

Thanks

submitted by /u/Neophyte-
[link] [comments]

Leave a Reply