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

Add support for running Generic Host based applications as a Windows Service #809

Closed
feco93 opened this Issue Jun 26, 2018 · 13 comments

Comments

Projects
None yet
6 participants
@feco93
Copy link

feco93 commented Jun 26, 2018

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

@Tratcher

This comment has been minimized.

Copy link
Member

Tratcher commented Jun 26, 2018

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.

@feco93

This comment has been minimized.

Copy link
Author

feco93 commented Jun 27, 2018

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()

@stevejgordon

This comment has been minimized.

Copy link
Contributor

stevejgordon commented Aug 11, 2018

@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".

@Tratcher

This comment has been minimized.

Copy link
Member

Tratcher commented Aug 11, 2018

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

@stevejgordon

This comment has been minimized.

Copy link
Contributor

stevejgordon commented Aug 13, 2018

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

@Tratcher

This comment has been minimized.

Copy link
Member

Tratcher commented Aug 13, 2018

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.

@stevejgordon

This comment has been minimized.

Copy link
Contributor

stevejgordon commented Aug 13, 2018

Ok thanks.

@davidfowl davidfowl changed the title RunAsService on IHost interface Add support for running Generic Host based applications as a Windows Service Sep 8, 2018

@aspnet-hello aspnet-hello transferred this issue from aspnet/Hosting Dec 18, 2018

@aspnet-hello aspnet-hello added this to the 3.0.0 milestone Dec 18, 2018

@Eilon Eilon removed the Extensions label Dec 18, 2018

@Tratcher Tratcher modified the milestones: 3.0.0, 3.0.0-preview3 Jan 17, 2019

@Tratcher Tratcher self-assigned this Jan 17, 2019

@Tratcher

This comment has been minimized.

Copy link
Member

Tratcher commented Jan 17, 2019

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?
@Tratcher

This comment has been minimized.

Copy link
Member

Tratcher commented Feb 2, 2019

@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...

@davidfowl

This comment has been minimized.

Copy link
Member

davidfowl commented Feb 2, 2019

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).

@davidfowl

This comment has been minimized.

Copy link
Member

davidfowl commented Feb 2, 2019

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.

@stevejgordon

This comment has been minimized.

Copy link
Contributor

stevejgordon commented Feb 4, 2019

@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.

@davidfowl

This comment has been minimized.

Copy link
Member

davidfowl commented Feb 4, 2019

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.

Tratcher added a commit that referenced this issue Feb 6, 2019

Tratcher added a commit that referenced this issue Feb 8, 2019

Tratcher added a commit that referenced this issue Feb 13, 2019

@Tratcher Tratcher added the 3 - Done label Feb 13, 2019

@Tratcher Tratcher closed this Feb 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.