-
Notifications
You must be signed in to change notification settings - Fork 307
Add support for hosting in a Windows service. #494
Conversation
Hi @easyt, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! TTYL, DNFBOT; |
Better, this is how a service should be written. What does it look like to consume it? Can you add a sample project that uses it? |
Oh wait, this repo has no samples because it can't depend on servers.... Maybe push a sample PR to Entropy? |
Take a look here: https://github.com/taskmatics/aspnet-windows-service/tree/aspnet-hosting-winsvc Note: There is a batch file (install.cmd) that uses the 'svc' command to run the application using this new service host. In VS you use the 'web' command. To be clear, the batch file does the following:
Also, now that I look at it, the --windows-service CLI option is not needed. It is left over from the previous implementation. Ignore it. |
}, | ||
|
||
"frameworks": { | ||
"dnx451": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have net451
, do you even need dnx451
?
I thought it was necessary to specify The 'commands' can for sure be removed. The project is intended to be used as the entry point of an application, similar to Microsoft.AspNet.Hosting. Although, a user can inherit from the Program class and override OnStart and OnStop if they want. |
@easyt shouldn't need it. |
{ | ||
// We are adding all environment variables first and then adding the ASPNET_ ones | ||
// with the prefix removed to unify with the command line and config file formats | ||
return new ConfigurationBuilder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a one line helper for this.
It would be easier if I could explain the intended usage over a quick screen-share, and you can give me guidelines to follow. It would save a lot of back and forth. Would that be possible? |
_application = host.Start(); | ||
_application.Services.GetRequiredService<IApplicationLifetime>().ApplicationStopped.Register(() => | ||
{ | ||
if (!_stopRequestedByWindows) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always add braces.
@davidfowl I think this is ready, no? |
@davidfowl Do you think we should take this as part of the product or a sample? Either way, I think we'll need tests with it as well. :) |
Yep this is good. |
@easyt Thanks for the contribution. Could you add some tests as well? |
The only way to test this fully is to perform an end-to-end test by installing a test service in Windows (using sc.exe) and hitting an endpoint. I can do this without a problem. However, the test runner will need to run with elevated permissions in order to call sc.exe. The test will also take a few seconds to complete since we would need to poll an endpoint up until a timeout period. Thoughts? |
|
||
public static void Run(WindowsServiceWebApplication app) | ||
{ | ||
Run(app); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
base.Run?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ServiceBase.Run will throw if it is not running within the context of a Windows service. It will fail if you just run it from the command line. You have to install it using sc.exe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this method recursive or is it calling the ServiceBase.Run?
@davidfowl @muratg Maybe this belongs in https://github.com/aspnet-contrib. It's a fully independent package for a specialized scenario. It also probably won't ever work on CoreCLR. |
It won't work on CoreCLR. It requires the full .NET framework. I personally, would like this feature to be included, and many others have requested this feature after I posted a blog about how it's possible to run ASP.NET in a Windows service. This makes it so the user doesn't have to code any of the hosting layer on their own. |
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this part is obsolete. The app will require its own Program.Main
in RC2.
private IApplication _application; | ||
private bool _stopRequestedByWindows; | ||
|
||
public static void Run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add XML doc comments.
(If you couldn't tell) @DamianEdwards loved this and wants it merged as soon as it has some sort of readme or doc comments. |
@JunTaoLuo Squash and merge this. Then update it as part of your other PR. |
I just noticed PR #521 from @davidfowl. I just pulled his changes and I have altered my code to make use of his changes. It actually simplifies things a bit. I will update this PR once I update the xml docs. |
/// <summary> | ||
/// Provides an implementation of a Windows service that hosts ASP.NET. | ||
/// </summary> | ||
/// <remarks> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove this as it'll get stale quickly. Leave this for docs on docs.asp.net
@easyt Can you rebase on dev? The new API is in. /cc @Tratcher and @JunTaoLuo make sure this goes in ASAP. |
/// </summary> | ||
public class WebApplicationService : ServiceBase | ||
{ | ||
private const string HostingJsonFile = "hosting.json"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you don't need any of these constants now.
Thanks @easyt! |
You guys are very welcome :) I'm glad to be helping out! |
@davidfowl: I think this is more inline with your thinking. LMK.