MassTransit Consumer中的异常冒泡会使Windows服务崩溃 [英] Exception bubbling in MassTransit Consumer crashes a Windows Service

查看:53
本文介绍了MassTransit Consumer中的异常冒泡会使Windows服务崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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 easiest thing is to just let the exception bubble out of your consumer method and MassTransit will automatically catch the exception for you

在使用者中引发异常时,MassTransit确实会产生故障,但我的服务仍然崩溃:

应用程序:WerkgeverService.exe框架版本: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,Boolean)在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean)在System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()在System.Threading.ThreadPoolWorkQueue.Dispatch()在System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

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()

因为我记录了所有未捕获的异常,所以记录了该异常:

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服务崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆