【发布时间】:2016-03-25 14:53:23
【问题描述】:
我正在运行 web api 2 作为 Windows 服务中的自托管,在停止期间我调用
this.webApp.Dispose();
但是,当我收到以下异常时,这看起来不足以优雅地停止服务:
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。
有没有办法优雅地停止服务,知道我在停止时没有运行长时间的任务?
【问题讨论】:
标签: c# windows-services asp.net-web-api2 self-hosting