2014-04-30 93 views
2

我使用AutoFac設置了2個使用者的Windows服務。在一個快樂的道路上,這很有效。我受到MassTransit爲我處理異常的困擾。 由於文檔狀態: http://docs.masstransit-project.com/en/latest/overview/errors.htmlMassTransit消費者中的異常冒泡Windows服務崩潰

最簡單的事情就是隻讓異常泡出你的 消費方法和MassTransit會自動捕獲異常 你

當異常被扔在消費者,MassTransit的確產生了一個錯誤,但我的服務仍然崩潰:

應用程序:WerkgeverService.exe Framework版本:v4.0.30319 描述:由於未處理的異常而終止進程。 異常信息:AutoMapper.AutoMapperMappingException堆棧:在 System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__5(System.Object的) 在 System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object的) 在 System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object的,布爾值)在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback,System.Object的,布爾型)在在System.Threading.ThreadPoolWorkQueue.Dispatch()上的System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 在 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

的記錄異常,因爲我記錄所有捕獲的異常:

Protected Overrides Sub OnStart(args() As String) 
     ' Add code here to start your service. This method should set things 
     ' in motion so your service can do its work. 
     Try 
      Initialize() 
      AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf HandleBubbledException 
     Catch ex As Exception 
      Me.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error) 
      Throw 
     End Try 
    End Sub 


    Private Sub HandleBubbledException(sender As Object, args As UnhandledExceptionEventArgs) 
     Dim exception = DirectCast(args.ExceptionObject, Exception) 
     Me.EventLog.WriteEntry(String.Format("Message: {0} {2}Stacktrace:{2}{1}", exception.Message, exception.StackTrace, Environment.NewLine), EventLogEntryType.Error) 
    End Sub 

例外本身是一個從AutoMapper確實在獲取記錄事件日誌(因此它不是由MassTransit處理):

Message: Missing type map configuration or unsupported mapping. 

Mapping types: 
Int32 -> Int64 
System.Int32 -> System.Int64 

Q UESTION:有沒有什麼方法可以防止服務崩潰而不會將try/catch添加到每個消費者? AFAIK MassTransit應該處理這些...

回答

2

這可能是特定於我的情況,但我想我找到了問題(感謝來自MassTransit的人)。

問題是我的消費者起動器有一個新的線程。

Public Sub Consume(message As IConsumeContext(Of IMessage)) Implements Consumes(Of IConsumeContext(Of IMessage)).All.Consume 
    HandleMessageAsync(message) 
End Sub 
Private Async Sub HandleMessageAsync(context As IConsumeContext(Of IMessage)) 
    Dim something = Await _controller.GetSomething(context.Message) 
    Dim response As New MessageResponse 
    response.something = something 
    context.Respond(response) 
End Sub 

此線程超出了MassTransit的掌握範圍,因此無法捕獲該線程的任何異常。對我來說,解決辦法是將它更改爲:

Public Sub Consume(context As IConsumeContext(Of IMessage)) Implements Consumes(Of IConsumeContext(Of IMessage)).All.Consume 
    Dim something = _controller.GetSomething(context.Message).Result 
    Dim response As New MessageResponse 
    response.something = something 
    context.Respond(response) 
End Sub 

只要確保異常的食用方法退出之前冒泡。否則,你必須自己去捕捉它。