From 9359606ee8a99289cbda476adc1670169cdec36b Mon Sep 17 00:00:00 2001 From: dudu Date: Sun, 5 Feb 2023 19:38:51 +0800 Subject: [PATCH 1/3] refactor: check if IEventBus is registered --- .../EndPointExtensions.cs | 9 +++++++++ .../Program.cs | 2 -- .../AssemblyInfo.cs | 2 +- .../Cnblogs.Architecture.UnitTests.csproj | 1 + .../EventBus/AssemblyAttributeTests.cs | 18 ++++++++++++++++-- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs index 6ca512b..da8bf81 100644 --- a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs +++ b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs @@ -2,6 +2,7 @@ using Cnblogs.Architecture.Ddd.EventBus.Abstractions; using Cnblogs.Architecture.Ddd.EventBus.Dapr; using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; // ReSharper disable once CheckNamespace namespace Microsoft.AspNetCore.Routing; @@ -62,6 +63,14 @@ public static IEndpointConventionBuilder Subscribe( where TEvent : IntegrationEvent { EnsureDaprSubscribeHandlerMapped(builder); + + var serviceCheck = builder.ServiceProvider.GetRequiredService(); + if (!serviceCheck.IsService(typeof(IEventBus))) + { + throw new InvalidOperationException( + $"{nameof(IEventBus)} has not been registered. Please using AddDaprEventBus to register."); + } + var result = builder .MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent)) .WithTopic(DaprOptions.PubSubName, DaprUtils.GetDaprTopicName(appName)); diff --git a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs index da3439a..bd06489 100644 --- a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs +++ b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs @@ -1,7 +1,5 @@ using Cnblogs.Architecture.Ddd.Cqrs.AspNetCore; -using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection; using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr; -using Cnblogs.Architecture.Ddd.EventBus.Dapr; using Cnblogs.Architecture.IntegrationTestProject.Application.Commands; using Cnblogs.Architecture.IntegrationTestProject.Application.Queries; using Cnblogs.Architecture.IntegrationTestProject.Payloads; diff --git a/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs b/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs index a804d50..3f39a5c 100644 --- a/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs +++ b/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs @@ -1,3 +1,3 @@ using Cnblogs.Architecture.Ddd.EventBus.Abstractions; -[assembly:AssemblyAppName("test")] \ No newline at end of file +[assembly: AssemblyAppName("test")] \ No newline at end of file diff --git a/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj b/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj index 654b351..3afa87b 100644 --- a/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj +++ b/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj @@ -15,6 +15,7 @@ + diff --git a/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs b/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs index 1c37ed7..2a7c7e7 100644 --- a/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs +++ b/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs @@ -1,9 +1,8 @@ using Cnblogs.Architecture.TestIntegrationEvents; - using FluentAssertions; - using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; namespace Cnblogs.Architecture.UnitTests.EventBus; @@ -14,6 +13,7 @@ public void SubscribeByAssemblyMeta_Success() { // Arrange var builder = WebApplication.CreateBuilder(); + builder.Services.AddDaprEventBus(nameof(AssemblyAttributeTests)); var app = builder.Build(); // Act @@ -22,4 +22,18 @@ public void SubscribeByAssemblyMeta_Success() // Assert act.Should().NotThrow(); } + + [Fact] + public void SubscribeByAssemblyMeta_Throw() + { + // Arrange + var builder = WebApplication.CreateBuilder(); + var app = builder.Build(); + + // Act + var act = () => app.Subscribe(); + + // Assert + act.Should().Throw(); + } } \ No newline at end of file From 61a5b694e6119e971848ea40682271f467a8cda3 Mon Sep 17 00:00:00 2001 From: dudu Date: Mon, 6 Feb 2023 12:25:13 +0800 Subject: [PATCH 2/3] refactor: tweak exception message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 沈星繁 --- .../EndPointExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs index da8bf81..dca799d 100644 --- a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs +++ b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs @@ -68,7 +68,7 @@ public static IEndpointConventionBuilder Subscribe( if (!serviceCheck.IsService(typeof(IEventBus))) { throw new InvalidOperationException( - $"{nameof(IEventBus)} has not been registered. Please using AddDaprEventBus to register."); + $"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?"); } var result = builder From 5270c2b7fd06c5df9f9a6bd7ac364ef268f6c339 Mon Sep 17 00:00:00 2001 From: dudu Date: Mon, 6 Feb 2023 12:33:12 +0800 Subject: [PATCH 3/3] refactor: introduce EnsureEventBusRegistered --- .../DaprOptions.cs | 4 ++- .../EndPointExtensions.cs | 35 +++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs index 6236c33..ce21a14 100644 --- a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs +++ b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs @@ -18,5 +18,7 @@ public class DaprOptions /// /// 是否调用过 app.MapSubscribeHandler() /// - internal static bool IsDaprSubscribeHandlerMapped { get; set; } + internal bool IsDaprSubscribeHandlerMapped { get; set; } + + internal bool IsEventBusRegistered { get; set; } } \ No newline at end of file diff --git a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs index dca799d..b22b860 100644 --- a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs +++ b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs @@ -3,6 +3,7 @@ using Cnblogs.Architecture.Ddd.EventBus.Dapr; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; // ReSharper disable once CheckNamespace namespace Microsoft.AspNetCore.Routing; @@ -62,14 +63,9 @@ public static IEndpointConventionBuilder Subscribe( string appName) where TEvent : IntegrationEvent { - EnsureDaprSubscribeHandlerMapped(builder); - - var serviceCheck = builder.ServiceProvider.GetRequiredService(); - if (!serviceCheck.IsService(typeof(IEventBus))) - { - throw new InvalidOperationException( - $"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?"); - } + var daprOptions = builder.ServiceProvider.GetRequiredService>().Value; + EnsureDaprSubscribeHandlerMapped(builder, daprOptions); + EnsureEventBusRegistered(builder, daprOptions); var result = builder .MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent)) @@ -104,9 +100,26 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly } } - private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder) + private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, DaprOptions daprOptions) + { + if (daprOptions.IsEventBusRegistered) + { + return; + } + + var serviceCheck = builder.ServiceProvider.GetRequiredService(); + if (!serviceCheck.IsService(typeof(IEventBus))) + { + throw new InvalidOperationException( + $"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?"); + } + + daprOptions.IsEventBusRegistered = true; + } + + private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder, DaprOptions daprOptions) { - if (DaprOptions.IsDaprSubscribeHandlerMapped) + if (daprOptions.IsDaprSubscribeHandlerMapped) { return; } @@ -117,6 +130,6 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build } builder.MapSubscribeHandler(); - DaprOptions.IsDaprSubscribeHandlerMapped = true; + daprOptions.IsDaprSubscribeHandlerMapped = true; } } \ No newline at end of file