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

Create Article to Show How to Run a DNX Application as a Windows Service #390

Closed
easyt opened this issue Aug 20, 2015 · 16 comments
Closed

Comments

@easyt
Copy link

easyt commented Aug 20, 2015

Damian Edwards told us, on the ASP.NET Community Standup this week, to post this issue. There should be an article describing how to run a DNX application (and subsequently ASP.NET 5) as a Windows service.

This is a use case for headless Windows services that need to expose an admin UI and choose to so though a web interface. We currently run our admin site for within our Windows service through a mini web server we developed.

Now, that ASP.NET can be hosted in a console application, it makes sense for us to move to that.

Currently there is no documentation about this scenario, so we came up with our own. You can base the official article on our write up: http://taskmatics.com/blog/run-dnx-applications-windows-service/

The article describes how to run a DNX app in a Windows service. We will have another follow up article explaining how to bootstrap ASP.NET 5 and MVC from within the service.

Thanks.

@easyt
Copy link
Author

easyt commented Aug 27, 2015

Here is a follow up articles that builds on the one above and shows how to host ASP.NET 5 with static files and MVC 6 in a Windows service: http://taskmatics.com/blog/host-asp-net-in-a-windows-service/

@danroth27
Copy link
Member

Sounds good. We would also welcome a PR on this one.

@BillDines
Copy link

Another vote for this. We have a product that runs on premise for each customer. It uses Windows Services hosting WCF with WPF clients. We want to add web UI's to our product using skill sets we are familiar with (.NET) but don't want to have to deploy IIS across our customer base to do it - that would be 000's of instances of IIS we have to worry about! ASP.NET hosted in a windows service would be perfect for us, but I am unsure whether this is an expected/supported deployment scenario for ASP.NET 5??

easyt has done some great work with his articles, but it still seems a bit hacky having to create a dnx console app and then add in ASP.NET manually - I would like to create a normal asp.net 5 project (so I can use all the tools available in VS to help me) and then run that as a windows service. I am trying to do this currently but it would be great to get an article from the experts!

thanks

@BillDines
Copy link

As I said above: "I would like to create a normal asp.net 5 project (so I can use all the tools available in VS to help me) and then run that as a windows service"

Well, its seems I can. You can just add a Program.cs class to your asp.net project something like this:

using System;
using System.ServiceProcess;
using System.Threading.Tasks;

public class Program : ServiceBase
{
private IServiceProvider serviceProvider;
private Task serverTask;

  public Program(IServiceProvider serviceProvider)
  {
     this.serviceProvider = serviceProvider;
  }
  public void Main(string[] args)
  {
     Run(this);
  }

  protected override void OnStart(string[] args)
  {
     base.OnStart(args);
     this.serverTask = Task.Run(() => StartWebServer());
  }

  private void StartWebServer()
  {
     new Microsoft.AspNet.Server.Kestrel.Program(serviceProvider).Main(new string[] { });
  }

  protected override void OnStop()
  {
     base.OnStop();

     if (this.serverTask != null)
     {
        this.serverTask.Dispose();
        this.serverTask = null;
     }
  }

}

And set up a new command in project.json:

"commands": {
...
"service": "namespace of your program.cs"
}

Note that this also works with the WebListener Service.
Note also that you have to run Kestrel/WebListener on a background thread as they block.

If you publish the project to the file system you can then run it as a windows service by referencing "service.cmd" as described by easyt in this article http://taskmatics.com/blog/host-asp-net-in-a-windows-service/.

Pretty simple and it works. During development you just work as if its a normal asp.net project, using IIS Express or console to host it. I've no idea if this is a good/robust way to do it though. e.g. there is hardly a graceful shutdown when you stop the service!

@easyt
Copy link
Author

easyt commented Nov 12, 2015

Bill,

Pull the code from this repo: https://github.com/taskmatics/aspnet-windows-service. It runs in VS by default using the command line. If you want to run it as a service, you can use install.cmd and attach VS to the running dnx.exe process. There is an uninstall.cmd that will remove the service as well, and you can tweak those scripts as necessary.

It also has a graceful shutdown of the web server.

@BillDines
Copy link

Hi Erez,

Thanks for getting in touch. I saw this very helpful example. In fact I may have given up without it! We may use this approach if it is the recommended way to host in a service. However, if it is possible, being able to just add a single class containing very little code to a standard ASP.NET project would be much better for us. I want to keep things as “normal” as possible so devs can learn from examples based on the standard project template and use all that comes with it (grunt, bower etc.). I realise you could probably set all this up in the console app project as well but it’s a lot of extra work and requires a lot more knowledge. The code I posted achieves the simplicity I am after, I’m just not sure how robust it is! I would be more comfortable if I was able to shutdown gracefully like in your example, but I haven’t found a way to do that yet. Maybe I could use a program.cs like yours inside the asp.net project to get more control over the shutdown…

Bill
From: Erez Testiler [mailto:[email protected]]
Sent: 12 November 2015 14:17
To: aspnet/Docs
Cc: Bill Dines
Subject: Re: [Docs] Create Article to Show How to Run a DNX Application as a Windows Service (#390)

Bill,

Pull the code from this repo: https://github.com/taskmatics/aspnet-windows-service. It runs in VS by default using the command line. If you want to run it as a service, you can use install.cmd and attach VS to the running dnx.exe process. There is an uninstall.cmd that will remove the service as well, and you can tweak those scripts as necessary.

It also has a graceful shutdown of the web server.


Reply to this email directly or view it on GitHubhttps://github.com//issues/390#issuecomment-156113215.

@BillDines
Copy link

Note that neither approach shown above will work in RC1 due to changes in the entrypoint semantics and how servers are started. see here

At first glance the changes look like it will be easier to run a dnx application up as a service, and I can get an asp.net MVC application to run as a service easily enough. Unfortunately it won't serve any pages at the moment! I will post here if I work out a complete solution...

@easyt
Copy link
Author

easyt commented Nov 24, 2015

The code in taskmatics/aspnet-windows-service has been updated to work on RC1. What issues are you seeing, exactly?

@easyt
Copy link
Author

easyt commented Nov 24, 2015

I have also provided a pull request to the aspnet/Hosting repo with a new project that will allow users to host ASP.NET in a Windows service with zero code:
aspnet/Hosting#494

The code example that uses this can be found at this branch:
https://github.com/taskmatics/aspnet-windows-service/tree/aspnet-hosting-winsvc

The install.cmd has been updated to use the 'svc' command which uses the new project I submitted (Microsoft.AspNet.Hosting.WindowsService) as the entry point.

@BillDines
Copy link

Your new code works with RC1. Sorry I missed that update. I checked your blog for updates, but not your repo. (I now realise I should have been watching it, but I am a Git Newbie!).

I still have a problem as I can't get it to run with WebListener (which I need for windows authentication - at least I think I do). That is not to do with your implementation I don't think, WebListener doesn't seem to work at all in RC1 - at least not for me! see here

The pull request looks great. It's exactly the way I would like it to work.

@danroth27 danroth27 added this to the Backlog milestone Dec 4, 2015
@easyt
Copy link
Author

easyt commented Dec 17, 2015

@danroth27, I have a pull request (aspnet/Hosting#494) that makes it much more simple to host ASP.NET in a Windows service.

I'd be glad to write this doc. I read CONTRIBUTING.md, forked the repo with a new branch and got sphinx-autobuild set up.

A couple questions:

  1. Where should this go?
    Options:
  • aspnet/fundamentals/windows-service-hosting.rst
  • aspnet/fundamentals/hosting/windows-service-hosting.rst
  • aspnet/fundamentals/hosting.rst
  • Is there someone I should talk to about the outline or should I just do a write up and submit a pull request and go from there?

Thanks.

@danroth27
Copy link
Member

@easyt Are you still interested in writing this doc? We would need it to be based on our RC2 bits. I would put it under Publishing and Deployment

@easyt
Copy link
Author

easyt commented May 19, 2016

Yes. Of course. I'd be glad to write it up. Do you have any guidelines to follow, or should I just go by the feel of the other articles on docs.asp.net?

@danroth27
Copy link
Member

See https://docs.asp.net/en/latest/contribute/index.html for our existing guidelines.

I recommend starting with an outline and posting it here so that we can give feedback before you write a bunch of content.

@danroth27
Copy link
Member

We now have a package available for this specific scenario: https://www.nuget.org/packages/Microsoft.AspNetCore.Hosting.WindowsServices/

@guardrex
Copy link
Collaborator

This looks to be superseded by #2467

@tdykstra tdykstra removed this from the Backlog milestone Mar 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants