简体   繁体   中英

Hosting ASP.NET5 Web Application in Windows Service

I have an existing Windows Service application, based on .NET Framework 4.5 and want to add an user interface for configuration and diagnostics.

In Windows Forms or WPF the service would send it's data via TCP connection (.NET Remoting or WCF) to the client, but that would require additional configuration, either on client or on server side and also requires service and client to be on the same compatibility level, which in worst case means multiple clients for multiple services.

I have an idea of melting service and user interface together in one application using new ASP.NET5 Web Application. When the Windows service starts, first I would like to start the web server and then start the rest of the service. So it would be possible for the user to interact with the service without installing any additional software. The Web Application and Windows Service should run in the same AppDomain, so that I can change an object in user interface and the service can use it immediately.

So far, I have played a bit with new Console Application (dnx451) and hosting via WebHostBuilder and got a working page, but I don't think that that's the solution I'm looking for. I get the project output and a cmd-file to start the server.

So my first question is: Is there an equivalent implementation of WebHostBuilder for .NET 4.5, to run a web site in tradional Visual Studio projects?

My second thought would be to completely run the service inside IIS, so I'd have a WebPage hosting the existing service functionality. But I'm not aware of any differences between applications in IIS and as Windows service. And what's about ASP.NET 4.6 in this case? Because I think I will have to convert my existing libraries into new ASP.NET5 class library (Packages).

The last possibility I could think of would be to publish web application to IIS and service as Windows service and communicate via WCF NamedPipes, like I would do with WPF or Windows Forms application.

To sum up, Is there anything planned to support my first idea or do I have to choose one of the others? And which option would probably be the best?

You can run ASP.NET 5 with .NET 4.5.1 using DNX451 (not DNX Core) and host it all using a Windows service. I did a write up of how to do this here: http://taskmatics.com/blog/host-asp-net-in-a-windows-service/

For .NET 4.5 your options are WebAPI 2.2 or NancyFX , both running on top of Microsoft.Owin.SelfHost . Unfortunately you can't self-host MVC 5.

For ASP.NET 5 w/ MVC 6 you're out of luck since it only runs on DNX (look at the dependencies in the MVC6 nuget package, they've only got references for DNX and DNXCore), and there is no windows service bootstraper for DNX. Also, deploying .NET 4.6 to production is not a good idea (yet).

If you want your service to be usable from the outside world, which I assume is true, I think the form needs to be hosted in ASP.NET, running within the IIS service.

However, you shouldn't need to do anything to your existing libraries; they should run as is in newer versions of the framework. As an example, I have a large collection of libraries that I built against version 2.0, and they run unchanged in 4.5. The only thing of which I am aware that might break yours is if they use functions that behave differently in the newer frameworks. So far, nothing like this has arisen with my libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM