diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/ExceptionHandling/ExceptionNotifier.cs b/framework/src/Volo.Abp.Core/Volo/Abp/ExceptionHandling/ExceptionNotifier.cs index b9076c471b..232b9a2ecd 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/ExceptionHandling/ExceptionNotifier.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/ExceptionHandling/ExceptionNotifier.cs @@ -1,7 +1,7 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using JetBrains.Annotations; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; @@ -12,11 +12,11 @@ namespace Volo.Abp.ExceptionHandling { public ILogger Logger { get; set; } - protected IEnumerable ExceptionSubscribers { get; } + protected IHybridServiceScopeFactory ServiceScopeFactory { get; } - public ExceptionNotifier(IEnumerable exceptionSubscribers) + public ExceptionNotifier(IHybridServiceScopeFactory serviceScopeFactory) { - ExceptionSubscribers = exceptionSubscribers; + ServiceScopeFactory = serviceScopeFactory; Logger = NullLogger.Instance; } @@ -24,16 +24,22 @@ namespace Volo.Abp.ExceptionHandling { Check.NotNull(context, nameof(context)); - foreach (var exceptionSubscriber in ExceptionSubscribers) + using (var scope = ServiceScopeFactory.CreateScope()) { - try - { - await exceptionSubscriber.HandleAsync(context); - } - catch (Exception e) + var exceptionSubscribers = scope.ServiceProvider + .GetServices(); + + foreach (var exceptionSubscriber in exceptionSubscribers) { - Logger.LogWarning($"Exception subscriber of type {exceptionSubscriber.GetType().AssemblyQualifiedName} has thrown an exception!"); - Logger.LogException(e, LogLevel.Warning); + try + { + await exceptionSubscriber.HandleAsync(context); + } + catch (Exception e) + { + Logger.LogWarning($"Exception subscriber of type {exceptionSubscriber.GetType().AssemblyQualifiedName} has thrown an exception!"); + Logger.LogException(e, LogLevel.Warning); + } } } }