如何从现有的Windows服务启动AspNetCore应用程序 [英] How to start a AspNetCore App from an existing windows service

查看:72
本文介绍了如何从现有的Windows服务启动AspNetCore应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Windows服务.目标是拥有REST接口以与此服务进行通信.我认为仅制作一个ASP.NET Core Web应用程序(请参见屏幕快照)并在现有服务中简单地进行整个操作可能是一个好主意.然后他们可以共享同一个IOC容器,依此类推.

I have an existing windows service. The goal is to have a REST interface to communicate with this service. I thought it might be a good idea to just make a ASP.NET Core Web Application (see screenshot) and simply start the whole thing inside my existing service. Then they could share the same IOC container and so on.

然后我使用 sc注册了服务,然后创建s1 binPath ="pathgoeshere"

当我启动服务(附加到调试过程)时,在输出窗口中出现错误:

When I start the service (attach to process for debugging) I get an error in the output window:

Exception thrown: 'System.DllNotFoundException' in Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll
The thread 0x5250 has exited with code 0 (0x0).
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
'WindowsService1.exe' (CLR v4.0.30319: WindowsService1.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceProcess.resources\v4.0_4.0.0.0_de_b03f5f7f11d50a3a\System.ServiceProcess.resources.dll'. Module was built without symbols.
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
The thread 0x4fa0 has exited with code 0 (0x0).
Microsoft.AspNetCore.Server.Kestrel: Warning: Unable to bind to http://localhost:5000 on the IPv4 loopback interface: 'Die DLL "libuv": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.'.
Exception thrown: 'System.DllNotFoundException' in Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll
The thread 0x58c4 has exited with code 0 (0x0).
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Microsoft.AspNetCore.Server.Kestrel: Warning: Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Die DLL "libuv": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.'.
Exception thrown: 'System.IO.IOException' in Microsoft.AspNetCore.Server.Kestrel.Core.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Microsoft.AspNetCore.Server.Kestrel: Critical: Unable to start Kestrel.

我将整个示例放在GitHub上:

I put the whole example on GitHub:

https://github.com/SuperSludge/aspnetcoreService

有人曾经做过这样的事情吗?我完全走错了路吗?将整个Windows服务重写为ASP.NET Core App是否更容易?

Has someone ever done something like this? Am I completely on the wrong path? Is it easier to rewrite the whole windows service as a ASP.NET Core App?

推荐答案

如果要在服务中包含REST Api,则应在服务项目中添加WebApi,而不要在其他项目中.因此,在这种情况下,我认为您不能使用Asp.Net Core.

If you want to have a REST Api in your service, you should add the WebApi in the service project not in a different project. So, in this case, I don't think you can use Asp.Net Core.

使用Owin在服务内部启动Rest Api,然后在另一个线程中运行服务.

Use Owin to launch the Rest Api inside the service and run the service in another thread.

// declare baseUrl in confing file by ex
// Startup is your Startup class
using (WebApp.Start<Startup>(baseUrl))
{
     System.Console.WriteLine($"Listening on {baseUrl}");
     Thread.Sleep(Timeout.Infinite);
}

Imo,您应该有另一个服务来运行此API,因此可以使用Asp.Net.Core.

Imo, you should have another service to run this API, so in that way, you can use Asp.Net.Core.

Windows服务执行一些逻辑,WebApi使用相同的Business Api来干扰相同的Db.

The Windows Service do some logic and the WebApi use the same Business Api to interfere with the same Db.

这篇关于如何从现有的Windows服务启动AspNetCore应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆