Nservicebus System.Net.HttpListenerException:未能侦听前缀';xxxx和x27;因为它与计算机上的现有注册冲突

Nservicebus System.Net.HttpListenerException:未能侦听前缀';xxxx和x27;因为它与计算机上的现有注册冲突,nservicebus,nservicebus3,nservicebus-distributor,Nservicebus,Nservicebus3,Nservicebus Distributor,无论何时部署我们的组件,我们都会收到以下异常。有时组件本身不会启动,有时服务控制台可能会说已启动,但服务不会处理任何消息。 这个错误非常简单。还有另一个HttpListener仍在侦听前缀 NServiceBus.Hosting.GenericHost System.Exception: Failed to start listener for http://localhost/products/ make sure that you have admin priviliges --->

无论何时部署我们的组件,我们都会收到以下异常。有时组件本身不会启动,有时服务控制台可能会说已启动,但服务不会处理任何消息。 这个错误非常简单。还有另一个HttpListener仍在侦听前缀

NServiceBus.Hosting.GenericHost System.Exception: Failed to start listener for http://localhost/products/ make sure that you have admin priviliges ---> System.Net.HttpListenerException: Failed to listen on prefix 'http://localhost:80/products/' because it conflicts with an existing registration on the machine.
   at System.Net.HttpListener.AddAllPrefixes()
   at System.Net.HttpListener.Start()
   at NServiceBus.Gateway.Channels.Http.HttpChannelReceiver.Start(String address, Int32 numWorkerThreads) in c:\BuildAgent\work\nsb.master_6\src\gateway\NServiceBus.Gateway\Channels\Http\HttpChannelReceiver.cs:line 30
   --- End of inner exception stack trace ---
   at NServiceBus.Gateway.Channels.Http.HttpChannelReceiver.Start(String address, Int32 numWorkerThreads) in c:\BuildAgent\work\nsb.master_6\src\gateway\NServiceBus.Gateway\Channels\Http\HttpChannelReceiver.cs:line 37
   at NServiceBus.Gateway.Receiving.GatewayReceiver.Start(Address localAddress) in c:\BuildAgent\work\nsb.master_6\src\gateway\NServiceBus.Gateway\Receiving\GatewayReceiver.cs:line 38
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at NServiceBus.Unicast.UnicastBus.NServiceBus.IStartableBus.Start(Action startupAction) in c:\BuildAgent\work\nsb.master_6\src\unicast\NServiceBus.Unicast\UnicastBus.cs:line 867
   at NServiceBus.Hosting.GenericHost.Start() in c:\BuildAgent\work\nsb.master_6\src\hosting\NServiceBus.Hosting\GenericHost.cs:line 34
当我们进行深入分析时

  • 此问题仅在运行NServiceBus.Master时发生 侧面图
  • 此问题仅在从服务控制台重新启动服务时发生
  • 无论您运行多少个WorkerThreads,都会出现问题
  • 这让我们感到困惑,当我们深入阅读NSB代码时,我们发现HttpListener在对象处理期间是关闭的。除此之外,当您停止服务时,整个进程本身将被终止,因此计算机上不会有任何对象侦听给定的URL前缀。但是,我们错了。当我们分析很多闭包时,发现即使服务控制台认为服务已停止,NServinceBus.Host进程仍在运行。每当我们在服务控制台上停止服务时,该进程在机器上仍然处于活动状态&完全关闭需要相当长的时间。所以,当我们执行重新启动时,当前服务可能有多个流程实例——一个正在尝试启动另一个正在尝试停止。我们确认重启进程引发了上述异常

    我们对正在关闭的进程进行了转储。我们发现,尽管服务控制台显示状态为stopped,但进程上仍有后台线程处于活动状态。在处置过程中有很多锁/ObjWait。请确保停止的服务尚未关闭HttpListener对象

            Child SP               IP Call Site
    000000001f19e048 00000000778818ca [GCFrame: 000000001f19e048] 
    000000001f19e118 00000000778818ca [HelperMethodFrame_1OBJ: 000000001f19e118] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object)
    000000001f19e230 000007fe99d57ccb System.Transactions.CommittableTransaction.Commit()
    000000001f19e2d0 000007fe99d577df System.Transactions.TransactionScope.InternalDispose()
    000000001f19e3c0 000007fe99d56f1c System.Transactions.TransactionScope.Dispose()
    000000001f19e590 000007fe99c4a3b5 NServiceBus.Utils.TransactionWrapper.RunInTransaction(System.Action, System.Transactions.IsolationLevel, System.TimeSpan)
    000000001f19e610 000007fe99c45b2b NServiceBus.Unicast.Transport.Transactional.TransactionalTransport.Process()
    000000001f19e680 000007fe99c455cb NServiceBus.Utils.WorkerThread.Loop()
    000000001f19e6d0 000007fef6fdf8a5 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
    000000001f19e830 000007fef6fdf609 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
    000000001f19e860 000007fef6fdf5c7 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
    000000001f19e8b0 000007fef6ff2d21 System.Threading.ThreadHelper.ThreadStart()
    000000001f19ebc8 000007fef81cf713 [GCFrame: 000000001f19ebc8] 
    000000001f19eef8 000007fef81cf713 [DebuggerU2MCatchHandlerFrame: 000000001f19eef8] 
    000000001f19f0d8 000007fef81cf713 [ContextTransitionFrame: 000000001f19f0d8] 
    000000001f19f2c8 000007fef81cf713 [DebuggerU2MCatchHandlerFrame: 000000001f19f2c8]
    
    我们发现,HttpListener是活动的&正在侦听

            Child SP               IP Call Site
    000000002038e2f8 000000007788135a [InlinedCallFrame: 000000002038e2f8] System.Net.UnsafeNclNativeMethods+HttpApi.HttpReceiveHttpRequest(System.Runtime.InteropServices.CriticalHandle, UInt64, UInt32, HTTP_REQUEST*, UInt32, UInt32*, System.Threading.NativeOverlapped*)
    000000002038e2f8 000007fe99c6a2f8 [InlinedCallFrame: 000000002038e2f8] System.Net.UnsafeNclNativeMethods+HttpApi.HttpReceiveHttpRequest(System.Runtime.InteropServices.CriticalHandle, UInt64, UInt32, HTTP_REQUEST*, UInt32, UInt32*, System.Threading.NativeOverlapped*)
    000000002038e2b0 000007fe99c6a2f8 DomainBoundILStubClass.IL_STUB_PInvoke(System.Runtime.InteropServices.CriticalHandle, UInt64, UInt32, HTTP_REQUEST*, UInt32, UInt32*, System.Threading.NativeOverlapped*)
    000000002038e3b0 000007fe99c697a4 System.Net.HttpListener.GetContext()
    000000002038e4d0 000007fe99c6924f NServiceBus.Gateway.Channels.Http.HttpChannelReceiver.HttpServer()
    000000002038e530 000007fef6fdf8a5 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
    000000002038e690 000007fef6fdf609 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
    000000002038e6c0 000007fef6fdf5c7 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
    000000002038e710 000007fef6ff2d21 System.Threading.ThreadHelper.ThreadStart()
    000000002038ea28 000007fef81cf713 [GCFrame: 000000002038ea28] 
    000000002038ed58 000007fef81cf713 [DebuggerU2MCatchHandlerFrame: 000000002038ed58] 
    000000002038ef38 000007fef81cf713 [ContextTransitionFrame: 000000002038ef38] 
    000000002038f128 000007fef81cf713 [DebuggerU2MCatchHandlerFrame: 000000002038f128]
    
    问题: 为什么服务控制台报告服务已停止,即使进程在机器上仍处于活动状态

    注意:若要重现此问题,您必须使用NServiceBus.Master配置文件将主机安装为Windows服务,然后尝试重新启动服务,您可能需要更多尝试才能解决此问题

    更新:

    当我在HttpChannelReceiver.Start()上读取NSB代码时,它会在新线程上启动侦听器

  • 此侦听器线程是前台线程。(所有其他工作线程都是BG线程)
  • 没有停止信号发送到此前台线程
  • 没有保存此线程引用的变量/字段-无法中止

  • 此侦听器线程仅在ServiceHost杀死当前进程的内存时被回收,直到ServicHost收集它,此线程将被阻止退出当前进程

    这应该已经在v4中修复,请参见

    这应该已经在v4中修复,请参见

    我在NSB上为此创建的问题。我在NSB上为此创建了一个问题。