diff --git a/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/Dispatch/DispatcherBase.cs b/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/Dispatch/DispatcherBase.cs index 3adc8766b..9d84af306 100644 --- a/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/Dispatch/DispatcherBase.cs +++ b/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/Dispatch/DispatcherBase.cs @@ -68,11 +68,11 @@ await executionStrategy.ExecuteAsync(strategyOptions, @event, async (@event) => if (dispatchRelation.CancelHandlers.Any()) await ExecuteEventCanceledHandlerAsync(serviceProvider, Logger, executionStrategy, dispatchRelation.CancelHandlers, @event); else - throw new Exception(ex.Message, ex); + ex.ThrowException(); } else { - Logger?.LogWarning("----- Publishing event {@Event} error rollback is ignored: message id: {messageId} -----", @event, @event.Id); + Logger?.LogError("----- Publishing event {@Event} error rollback is ignored: message id: {messageId} -----", @event, @event.Id); } }); } @@ -96,9 +96,9 @@ await executionStrategy.ExecuteAsync(strategyOptions, @event, async @event => }, (@event, ex, failureLevels) => { if (failureLevels != FailureLevels.Ignore) - throw new Exception(ex.Message, ex); + ex.ThrowException(); - logger?.LogWarning("----- Publishing event {@Event} rollback error ignored: message id: {messageId} -----", @event, @event.Id); + logger?.LogError("----- Publishing event {@Event} rollback error ignored: message id: {messageId} -----", @event, @event.Id); return Task.CompletedTask; }); } diff --git a/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/DispatcherExtensions.cs b/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/DispatcherExtensions.cs index edc887265..0eeedbfab 100644 --- a/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/DispatcherExtensions.cs +++ b/src/Dispatcher/Masa.Contrib.Dispatcher.Events/Internal/DispatcherExtensions.cs @@ -21,4 +21,13 @@ public static IServiceCollection TryAdd(this IServiceCollection services, Type s public static bool IsGenericInterfaceAssignableFrom(this Type eventHandlerType, Type type) => type.IsConcrete() && type.GetInterfaces().Any(t => t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == eventHandlerType); + + /// + /// Keep the original stack information and throw an exception + /// + /// + public static void ThrowException(this Exception exception) + { + ExceptionDispatchInfo.Capture(exception).Throw(); + } } diff --git a/src/Dispatcher/Masa.Contrib.Dispatcher.Events/_Imports.cs b/src/Dispatcher/Masa.Contrib.Dispatcher.Events/_Imports.cs index 13726e3b8..9ab046657 100644 --- a/src/Dispatcher/Masa.Contrib.Dispatcher.Events/_Imports.cs +++ b/src/Dispatcher/Masa.Contrib.Dispatcher.Events/_Imports.cs @@ -15,4 +15,5 @@ global using Microsoft.Extensions.Options; global using System.Linq.Expressions; global using System.Reflection; +global using System.Runtime.ExceptionServices; global using System.Text.Json.Serialization; diff --git a/test/Masa.Contrib.Data.UoW.EF.Tests/TestUnitOfWork.cs b/test/Masa.Contrib.Data.UoW.EF.Tests/TestUnitOfWork.cs index 11a90e8c7..7fb9624e5 100644 --- a/test/Masa.Contrib.Data.UoW.EF.Tests/TestUnitOfWork.cs +++ b/test/Masa.Contrib.Data.UoW.EF.Tests/TestUnitOfWork.cs @@ -58,7 +58,7 @@ public void TestTransaction() [TestMethod] public async Task TestUseTranscationAsync() { - _options.Object.UseUoW(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + _options.Object.UseUoW(options => options.UseSqlite(Connection)); var serviceProvider = _options.Object.Services.BuildServiceProvider(); var dbContext = serviceProvider.GetRequiredService(); await dbContext.Database.EnsureCreatedAsync(); @@ -79,7 +79,7 @@ public async Task TestUseTranscationAsync() [TestMethod] public async Task TestNotUseTranscationAsync() { - _options.Object.UseUoW(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + _options.Object.UseUoW(options => options.UseSqlite(Connection)); var serviceProvider = _options.Object.Services.BuildServiceProvider(); var dbContext = serviceProvider.GetRequiredService(); await dbContext.Database.EnsureCreatedAsync(); @@ -108,7 +108,7 @@ public async Task TestNotTransactionCommitAsync() [TestMethod] public async Task TestCommitAsync() { - _options.Object.UseUoW(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + _options.Object.UseUoW(options => options.UseSqlite(Connection)); var serviceProvider = _options.Object.Services.BuildServiceProvider(); var dbContext = serviceProvider.GetRequiredService(); await dbContext.Database.EnsureCreatedAsync(); @@ -128,7 +128,7 @@ public async Task TestCommitAsync() [TestMethod] public async Task TestOpenRollbackAsync() { - _options.Object.UseUoW(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + _options.Object.UseUoW(options => options.UseSqlite(Connection)); var serviceProvider = _options.Object.Services.BuildServiceProvider(); var dbContext = serviceProvider.GetRequiredService(); await dbContext.Database.EnsureCreatedAsync(); @@ -145,7 +145,7 @@ public async Task TestOpenRollbackAsync() public async Task TestAddLoggerAndOpenRollbackAsync() { _options.Object.Services.AddLogging(); - _options.Object.UseUoW(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + _options.Object.UseUoW(options => options.UseSqlite(Connection)); var serviceProvider = _options.Object.Services.BuildServiceProvider(); var dbContext = serviceProvider.GetRequiredService(); await dbContext.Database.EnsureCreatedAsync(); diff --git a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/BaseRepositoryTest.cs b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/BaseRepositoryTest.cs index 0e75231af..a84ca36bd 100644 --- a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/BaseRepositoryTest.cs +++ b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/BaseRepositoryTest.cs @@ -72,7 +72,7 @@ public void TestAddMultRepository() { _dispatcherOptions.Setup(option => option.Assemblies).Returns(_assemblies).Verifiable(); _services.AddScoped(typeof(IUnitOfWork), _ => _uoW.Object); - _services.AddMasaDbContext(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + _services.AddMasaDbContext(options => options.UseSqlite(Connection)); _dispatcherOptions.Object.UseRepository().UseRepository(); var serviceProvider = _services.BuildServiceProvider(); diff --git a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/Masa.Contrib.Ddd.Domain.Repository.EF.Tests.csproj b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/Masa.Contrib.Ddd.Domain.Repository.EF.Tests.csproj index 803706330..0a68300a5 100644 --- a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/Masa.Contrib.Ddd.Domain.Repository.EF.Tests.csproj +++ b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/Masa.Contrib.Ddd.Domain.Repository.EF.Tests.csproj @@ -12,6 +12,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/RepositoryTest.cs b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/RepositoryTest.cs index 4bf7eaa10..9d7d59fce 100644 --- a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/RepositoryTest.cs +++ b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/RepositoryTest.cs @@ -26,7 +26,7 @@ public async Task InitializeAsync(Action? action) _dispatcherOptions.Setup(options => options.Services).Returns(() => _services); _dispatcherOptions.Setup(options => options.Assemblies).Returns(() => _assemblies); if (action == null) - _services.AddMasaDbContext(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + _services.AddMasaDbContext(options => options.UseSqlite(Connection)); else action.Invoke(_services); @@ -364,7 +364,7 @@ public async Task TestDbTransactionAsync() public async Task TestServiceLifeAsync() { var services = new ServiceCollection(); - services.AddMasaDbContext(options => options.DbContextOptionsBuilder.UseSqlite(Connection)); + services.AddMasaDbContext(options => options.UseSqlite(Connection)); var serviceProvider = services.BuildServiceProvider(); await using (var scope = serviceProvider.CreateAsyncScope()) diff --git a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/_Imports.cs b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/_Imports.cs index 788bb0859..d1e506f12 100644 --- a/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/_Imports.cs +++ b/test/Masa.Contrib.Ddd.Domain.Repository.EF.Tests/_Imports.cs @@ -10,6 +10,7 @@ global using Masa.Contrib.Ddd.Domain.Repository.EF.Tests.Domain.Repositories; global using Masa.Contrib.Ddd.Domain.Repository.EF.Tests.Infrastructure; global using Masa.Utils.Data.EntityFrameworkCore; +global using Masa.Utils.Data.EntityFrameworkCore.Sqlite; global using Microsoft.Data.Sqlite; global using Microsoft.EntityFrameworkCore; global using Microsoft.EntityFrameworkCore.Storage; diff --git a/test/Masa.Contrib.Dispatcher.Events.Tests/EventHandlers/RegisterUserEventHandler.cs b/test/Masa.Contrib.Dispatcher.Events.Tests/EventHandlers/RegisterUserEventHandler.cs new file mode 100644 index 000000000..d6f3e619c --- /dev/null +++ b/test/Masa.Contrib.Dispatcher.Events.Tests/EventHandlers/RegisterUserEventHandler.cs @@ -0,0 +1,11 @@ +namespace Masa.Contrib.Dispatcher.Events.Tests.EventHandlers; + +public class RegisterUserEventHandler +{ + [EventHandler] + public void RegisterUser(RegisterUserEvent registerUserEvent) + { + throw new NotSupportedException(); + } +} + diff --git a/test/Masa.Contrib.Dispatcher.Events.Tests/Events/RegisterUserEvent.cs b/test/Masa.Contrib.Dispatcher.Events.Tests/Events/RegisterUserEvent.cs new file mode 100644 index 000000000..515317452 --- /dev/null +++ b/test/Masa.Contrib.Dispatcher.Events.Tests/Events/RegisterUserEvent.cs @@ -0,0 +1,5 @@ +namespace Masa.Contrib.Dispatcher.Events.Tests.Events; + +public record RegisterUserEvent(string Name) : Event +{ +} diff --git a/test/Masa.Contrib.Dispatcher.Events.Tests/FeaturesTest.cs b/test/Masa.Contrib.Dispatcher.Events.Tests/FeaturesTest.cs index faf0da851..2bbd54ffb 100644 --- a/test/Masa.Contrib.Dispatcher.Events.Tests/FeaturesTest.cs +++ b/test/Masa.Contrib.Dispatcher.Events.Tests/FeaturesTest.cs @@ -1,5 +1,3 @@ -using Masa.Contrib.Dispatcher.Events.HandlerOrder.Tests.Events; - namespace Masa.Contrib.Dispatcher.Events.Tests; [TestClass] @@ -365,6 +363,16 @@ public void TestOrderLessThanZero() Assert.ThrowsException(() => { new EventHandlerAttribute(-10); - },"The order must be greater than or equal to 0"); + }, "The order must be greater than or equal to 0"); + } + + [TestMethod] + public async Task TestEventBusExceptionAsync() + { + var services = new ServiceCollection(); + services.AddEventBus(); + var registerUserEvent = new RegisterUserEvent("Jim"); + var eventBus = services.BuildServiceProvider().GetRequiredService(); + await Assert.ThrowsExceptionAsync(async () => await eventBus.PublishAsync(registerUserEvent)); } } diff --git a/test/Masa.Contrib.Dispatcher.Events.Tests/SagaTest.cs b/test/Masa.Contrib.Dispatcher.Events.Tests/SagaTest.cs index 6630a0fd3..c2cc546fe 100644 --- a/test/Masa.Contrib.Dispatcher.Events.Tests/SagaTest.cs +++ b/test/Masa.Contrib.Dispatcher.Events.Tests/SagaTest.cs @@ -64,14 +64,7 @@ public async Task TestMultiHandlerBySaga(string account, string optAccount, stri }; if (isError == 1) { - try - { - await _eventBus.PublishAsync(@event); - } - catch (Exception ex) - { - Assert.IsTrue(ex.InnerException is NotSupportedException); - } + await Assert.ThrowsExceptionAsync(async () => await _eventBus.PublishAsync(@event)); } else { diff --git a/test/Masa.Contrib.Dispatcher.Events.Tests/_Imports.cs b/test/Masa.Contrib.Dispatcher.Events.Tests/_Imports.cs index df4d04cb9..bce2f5109 100644 --- a/test/Masa.Contrib.Dispatcher.Events.Tests/_Imports.cs +++ b/test/Masa.Contrib.Dispatcher.Events.Tests/_Imports.cs @@ -6,7 +6,7 @@ global using Masa.Contrib.Dispatcher.Events.CheckMethodsParameterType.Tests.Events; global using Masa.Contrib.Dispatcher.Events.CheckMethodsType.Tests.Events; global using Masa.Contrib.Dispatcher.Events.Enums; -global using Masa.Contrib.Dispatcher.Events.Options; +global using Masa.Contrib.Dispatcher.Events.HandlerOrder.Tests.Events; global using Masa.Contrib.Dispatcher.Events.OrderEqualBySaga.Tests.Events; global using Masa.Contrib.Dispatcher.Events.Tests.Events; global using Masa.Contrib.Dispatcher.Events.Tests.Middleware; diff --git a/test/Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF.Tests/IntegrationEventLogServiceTest.cs b/test/Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF.Tests/IntegrationEventLogServiceTest.cs index 333021e35..a0d18f677 100644 --- a/test/Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF.Tests/IntegrationEventLogServiceTest.cs +++ b/test/Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF.Tests/IntegrationEventLogServiceTest.cs @@ -50,7 +50,7 @@ public async Task TestRetrieveEventLogsFailedToPublishAsync() { var dispatcherOptions = CreateDispatcherOptions(new ServiceCollection()); dispatcherOptions.UseEventLog(); - dispatcherOptions.Services.AddMasaDbContext(option => option.DbContextOptionsBuilder.UseSqlite(Connection)); + dispatcherOptions.Services.AddMasaDbContext(option => option.UseSqlite(Connection)); dispatcherOptions.Services.AddScoped(); var serviceProvider = dispatcherOptions.Services.BuildServiceProvider(); await serviceProvider.GetRequiredService().Database.EnsureCreatedAsync(); @@ -276,7 +276,7 @@ public async Task TestMarkEventAsFailed2Async() var dispatcherOptions = CreateDispatcherOptions(new ServiceCollection()); dispatcherOptions.UseEventLog(); dispatcherOptions.Services.AddMasaDbContext(option => - option.DbContextOptionsBuilder.UseSqlite(Connection)); + option.UseSqlite(Connection)); dispatcherOptions.Services.AddScoped(); Mock integrationEventBus = new(); var types = AppDomain.CurrentDomain.GetAssemblies()