简体   繁体   English

无法停止Windows服务托管Web API

[英]Unable to Stop Windows Service Hosting Web API

I am running web api 2 as a self hosted in a windows service, during the stop I call this.webApp.Dispose(); 我将Web api 2作为Windows this.webApp.Dispose();服务中的自托管服务器运行,在停止期间,我将其称为this.webApp.Dispose();

However this looks not enough to gracefully stopping the service as I get the following exception: 但是,这似乎不足以优雅地停止服务,因为我收到以下异常:

Failed to stop service. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.HttpListenerException: Failed to listen on prefix 'http://+:8080/' because it conflicts with an existing registration on the machine.
   at System.Net.HttpListener.AddAllPrefixes()
   at System.Net.HttpListener.Start()
   at Microsoft.Owin.Host.HttpListener.OwinHttpListener.Start(HttpListener listener, Func`2 appFunc, IList`1 addresses, IDictionary`2 capabilities, Func`2 loggerFactory)
   at Microsoft.Owin.Host.HttpListener.OwinServerFactory.Create(Func`2 app, IDictionary`2 properties)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, Cu...

So the only way to stop the service is to get PID for that service and then use kill-process. 因此,停止服务的唯一方法是获取该服务的PID,然后使用kill-process。

Is there a way to gracefully stop the service, knowing that I am not running a long task on stop? 知道我没有在停止时执行长任务,是否有办法优雅地停止服务?

发现在stop() Windows服务期间,我实际上是在调用OnStart事件,该事件将启动Web api而不是停止它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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