2016-03-25 39 views
0

我正在运行的Web API 2作为一个窗口服务托管的自我,停止我打电话 this.webApp.Dispose();无法停止Windows服务托管的Web API

期间然而这看起来不够优雅地停止服务,因为我得到以下例外:

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

因此,停止服务的唯一方法是获取该服务的PID,然后使用kill-process。

有没有办法优雅地停止服务,知道我没有在停止运行一项长期任务?

回答

0

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