Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 6 install as a Windows Service (ASP.NET Core 1.0.0)

UPDATE - 26th July 2016

I have added the solution to this in ASP.NET Core 1.0.0 in the answers below.


I have created a simple MVC 6 app and have included the Microsoft.AspNet.WebListener library so I can host outside of IIS.

From project.json:

"dependencies": {
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
    "Microsoft.AspNet.Mvc": "6.0.0-beta4"
},

"commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
}

When I publish this I can run the web.cmd file and get the site running in a console window. Great!

But in OWIN you can use TopShelf to launch your web app from a Console Application. This can then be built as an executable and installed as a Windows Service.

Is there a way to do this with an ASP.NET 5 MVC 6 web app?

like image 853
JDTLH9 Avatar asked May 16 '15 16:05

JDTLH9


People also ask

Does .NET Core support Windows service?

NET Core and . NET 5+, developers who relied on . NET Framework could create Windows Services to perform background tasks or execute long-running processes. This functionality is still available and you can create Worker Services that run as a Windows Service.


1 Answers

You can run a DNX app as a Windows service, however, you can't run the CMD file directly. You will get an error saying the following: 'The service did not respond to the start or control request in a timely fashion.' You can point directly to dnx.exe and pass the project folder and command as arguments.

Read this post for a lot more detail: http://taskmatics.com/blog/run-dnx-applications-windows-service/

Once you have your app set up. You can bootstrap ASP.NET from the OnStart method of the service. To do this you can use WebHostBuilder from Microsoft.AspNet.Hosting.

Lastly, you can ensure the app is still runnable in VS by passing an argument (such as 'non-service') to the Main method and check that before calling ServiceBase.Run, and if present, you can call OnStart directly instead. The project's properties gives you the option to pass arguments when running in VS.

UPDATE:

There is a follow up post which builds upon the one above. It shows how to run ASP.NET 5 with static files and MVC 6 in a Windows service. The link is here: http://taskmatics.com/blog/host-asp-net-in-a-windows-service/

like image 129
Erez T Avatar answered Oct 25 '22 05:10

Erez T