Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Add support for hosting in a Windows service. #494

Closed
wants to merge 1 commit into from
Closed

Conversation

easyt
Copy link

@easyt easyt commented Nov 20, 2015

@davidfowl: I think this is more inline with your thinking. LMK.

@dnfclas
Copy link

dnfclas commented Nov 20, 2015

Hi @easyt, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!
We will now validate the agreement and then real humans will evaluate your PR.

TTYL, DNFBOT;

@Tratcher
Copy link
Member

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?

@Tratcher
Copy link
Member

Oh wait, this repo has no samples because it can't depend on servers.... Maybe push a sample PR to Entropy?

@easyt
Copy link
Author

easyt commented Nov 21, 2015

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:

  1. dnu restore
  2. dnu publish
  3. Create the service and point to the svc.cmd in the publish output.

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": {
Copy link
Member

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?

@easyt
Copy link
Author

easyt commented Nov 21, 2015

I thought it was necessary to specify dnx451 if you choose to use it as the entry point of an application.

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.

@Eilon
Copy link
Member

Eilon commented Nov 21, 2015

@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()
Copy link
Member

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.

@easyt
Copy link
Author

easyt commented Nov 22, 2015

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always add braces.

@Tratcher
Copy link
Member

Tratcher commented Dec 7, 2015

@davidfowl I think this is ready, no?

@muratg
Copy link

muratg commented Dec 8, 2015

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

@davidfowl
Copy link
Member

Yep this is good.

@muratg muratg added this to the 1.0.0-rc2 milestone Dec 9, 2015
@muratg
Copy link

muratg commented Dec 9, 2015

@easyt Thanks for the contribution. Could you add some tests as well?

@easyt
Copy link
Author

easyt commented Dec 9, 2015

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base.Run?

Copy link
Author

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.

Copy link
Member

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?

@Tratcher
Copy link
Member

Tratcher commented Dec 9, 2015

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

@easyt
Copy link
Author

easyt commented Dec 9, 2015

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)
Copy link
Member

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()
Copy link
Member

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.

@Tratcher
Copy link
Member

(If you couldn't tell) @DamianEdwards loved this and wants it merged as soon as it has some sort of readme or doc comments.

@Tratcher Tratcher assigned JunTaoLuo and unassigned Tratcher Dec 12, 2015
@Tratcher
Copy link
Member

@JunTaoLuo Squash and merge this. Then update it as part of your other PR.

@easyt
Copy link
Author

easyt commented Dec 15, 2015

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>
Copy link
Member

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

@davidfowl
Copy link
Member

@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";
Copy link
Member

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.

@Tratcher
Copy link
Member

:shipit: @JunTaoLuo

@JunTaoLuo
Copy link
Contributor

Merged in 83c8816. Thanks for the contribution @easyt!

@JunTaoLuo JunTaoLuo closed this Dec 23, 2015
@muratg
Copy link

muratg commented Dec 23, 2015

Thanks @easyt!

@easyt
Copy link
Author

easyt commented Dec 23, 2015

You guys are very welcome :) I'm glad to be helping out!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants