Extensions: Add support for running Generic Host based applications as a Windows Service

Created on 26 Jun 2018  路  13Comments  路  Source: dotnet/extensions

Currently RunAsService depends on IWebHost. It should be depend on IHost interface, because it would be great if anyone could write windows service without the HTTP infrastructure.

There is an SO post, which about this feature request:
https://stackoverflow.com/questions/50848141/netcore-2-1-generic-host-as-a-service

3 - Done area-hosting enhancement

Most helpful comment

Good to know, we're shipping it as part of preview3. We're just trying to figure out how to make it a bit easier to use.

All 13 comments

This works a bit differently in GenericHost. See https://github.com/aspnet/Hosting/blob/2a98db6a73512b8e36f55a1e6678461c34f4cc4d/samples/GenericHostSample/ServiceBaseLifetime.cs#L13 Though there's still room for improvement.

Thanks, it works like a charm.

Yes, it would be great if this solution could work part of the framework.

Edit:
Unfortunately, it doesn't work on MAC os:
Unhandled Exception: System.PlatformNotSupportedException: ServiceController enables manipulating and accessing Windows services and it is not applicable for other operating systems.
at System.ServiceProcess.ServiceBase..ctor()

@Tratcher Is there a plan to include the ServiceBaseLifetime.cs and ServiceBaseLifetimeHostExtensions.cs within Microsoft.AspNetCore.Hosting.WindowsServices in the future (perhaps 2.2)? I've just tested a windows service based on the sample and it seems to work. Would be great if it was "built-in".

It's likely. We'll be doing a lot more work in this space in 3.0.

Thanks @Tratcher. Are there any tracking issues for the 3.0 plans available that I can follow?

We haven't finalized our 3.0 plans yet, but keep track of this one and we'll updated it as we work things out.

Ok thanks.

Try to build in all the workarounds from https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.2

  • current directory
  • only register the service lifetime if in a service, otherwise console
  • AddEventLog?

@davidfowl AddEventLog turns out to be very useful, but it's also awkward to include by default because you can only configure it in the AddEventLog call. How about we add that to the docs instead?

The ContentRootPath workaroud isn't required, we already set it in generic host.
ContentRootPath = ResolveContentRootPath(_hostConfiguration[HostDefaults.ContentRootKey], AppContext.BaseDirectory),
We don't want to set CurrentDirectory, right?

Detecting if we're in a service turns out to be quite difficult...

We can consider turning this on by default in CreateDefaultBuilder if running on windows and for errors only (it's useful for ASP.NET Core applications as well).

The ContentRootPath workaroud isn't required, we already set it in generic host.
ContentRootPath = ResolveContentRootPath(_hostConfiguration[HostDefaults.ContentRootKey], AppContext.BaseDirectory),
We don't want to set CurrentDirectory, right?

No, I don't think we want to do that (even though we're doing it in the IIS case). We can doc it though.

@davidfowl @Tratcher - Just FYI, my blog post on achieving this (sort of) with the code from Microsoft.AspNetCore.Hosting.WindowsServices is one of the highest viewed posts monthly at the moment, so it would suggest quite a high customer interest in this feature. Not that I use it personally as I'm all about the Linux containers; I'm happy to update the information or add a new post once something baked in is available in the previews.

Good to know, we're shipping it as part of preview3. We're just trying to figure out how to make it a bit easier to use.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shirhatti picture shirhatti  路  6Comments

vanbukin picture vanbukin  路  7Comments

pranavkm picture pranavkm  路  7Comments

mvrmoreira picture mvrmoreira  路  3Comments

christiannagel picture christiannagel  路  4Comments