AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

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

Open easyt opened this issue 11 years ago • 16 comments

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 avatar Aug 20 '15 17:08 easyt

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/

easyt avatar Aug 27 '15 18:08 easyt

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

danroth27 avatar Aug 28 '15 22:08 danroth27

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 avatar Nov 12 '15 09:11 BillDines

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!

BillDines avatar Nov 12 '15 14:11 BillDines

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.

easyt avatar Nov 12 '15 14:11 easyt

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/aspnet/Docs/issues/390#issuecomment-156113215.

BillDines avatar Nov 12 '15 15:11 BillDines

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

BillDines avatar Nov 24 '15 17:11 BillDines

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

easyt avatar Nov 24 '15 17:11 easyt

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: https://github.com/aspnet/Hosting/pull/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.

easyt avatar Nov 24 '15 17:11 easyt

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.

BillDines avatar Nov 25 '15 09:11 BillDines

@danroth27, I have a pull request (https://github.com/aspnet/Hosting/pull/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.

easyt avatar Dec 17 '15 17:12 easyt

@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

danroth27 avatar May 19 '16 06:05 danroth27

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?

easyt avatar May 19 '16 12:05 easyt

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 avatar May 21 '16 05:05 danroth27

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

danroth27 avatar Sep 29 '16 18:09 danroth27

This looks to be superseded by https://github.com/aspnet/Docs/issues/2467

guardrex avatar Jan 24 '17 08:01 guardrex