gpt4 book ai didi

.net - MassTransit Consumer中的异常冒泡会使Windows服务崩溃

转载 作者:行者123 更新时间:2023-12-03 13:00:18 25 4
gpt4 key购买 nike

我使用AutoFac与2个使用者建立了Windows服务。在一条快乐的道路上,这确实非常有效。我不敢相信MassTransit为我处理异常。
如文档所述:
http://docs.masstransit-project.com/en/latest/overview/errors.html

The easiest thing is to just let the exception bubble out of your consumer method and MassTransit will automatically catch the exception for you



当使用者中引发异常时,MassTransit确实会产生错误,但是我的服务仍然崩溃:

Application: WerkgeverService.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: AutoMapper.AutoMapperMappingException Stack: at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__5(System.Object) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at 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

问题:是否可以在不向每个使用者添加 try/catch的情况下防止服务崩溃? AFAIK MassTransit应该处理这些...

最佳答案

这可能是我的具体情况,但我想我发现了问题(感谢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

只需确保在Consume方法退出之前该异常冒泡即可。否则,您必须自己捕获它。

关于.net - MassTransit Consumer中的异常冒泡会使Windows服务崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23383538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com