问题描述
我使用AutoFac与2个使用者建立了Windows服务.在一条快乐的道路上,这确实非常有效.我不敢相信MassTransit为我处理异常.如文档所述: http://docs.masstransit-project.com/en/latest/overview/errors.html
I set up a windows service with 2 consumers using AutoFac. In a happy path, this works really well. I was under the impresison that MassTransit handled exceptions for me.As the docs state:http://docs.masstransit-project.com/en/latest/overview/errors.html
在使用者中引发异常时,MassTransit确实会产生故障,但我的服务仍然崩溃:
因为我记录了所有未捕获的异常,所以记录了该异常:
The exception is logged because I log all uncaught exceptions:
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处理):
The exception itself is one from in AutoMapper and indeed gets logged in the eventlog (so it is not handled by MassTransit):
Message: Missing type map configuration or unsupported mapping.
Mapping types:
Int32 -> Int64
System.Int32 -> System.Int64
问题:是否可以在不向每个使用者添加 try/catch
的情况下阻止服务崩溃?AFAIK MassTransit应该处理这些...
QUESTION: Is there any way in which I can prevent the service from crashing without adding try/catch
to every consumer? AFAIK MassTransit should handle these...
推荐答案
这可能是我的具体情况,但我认为我发现了问题(感谢MassTransit的同事).
This might be specific for my case, but I think I found the problem (thanks to the guys from MassTransit).
问题是我的使用者启动了一个新线程.
The problem was that my consumer starter a new thread.
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的控制范围,因此它无法捕获该线程的任何异常.对我来说,解决方案是将其更改为:
This thread lives out of the grasp of MassTransit so it can't catch any exception from that thread. The solution for me was to change it to:
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
只需确保在Consume方法退出之前气泡冒泡.否则,您必须自己抓住它.
Just make sure the exception bubbles up before the Consume method exits. Otherwise you have to catch it yourself.
这篇关于MassTransit Consumer中的异常冒泡会使Windows服务崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!