From 95082ecf64c48db18e36fc701dad110056b85be0 Mon Sep 17 00:00:00 2001 From: dudu Date: Wed, 8 Feb 2023 12:59:26 +0800 Subject: [PATCH] feat: register DaprEventBus with integration event handlers --- ...DaprEventBusServiceCollectionExtensions.cs | 15 +++++++++++++- .../Program.cs | 8 +++----- .../IntegrationEventHandlerTests.cs | 20 ++++++++++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr/DaprEventBusServiceCollectionExtensions.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr/DaprEventBusServiceCollectionExtensions.cs index a370748..2bff854 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr/DaprEventBusServiceCollectionExtensions.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr/DaprEventBusServiceCollectionExtensions.cs @@ -1,6 +1,8 @@ using Cnblogs.Architecture.Ddd.EventBus.Abstractions; using Cnblogs.Architecture.Ddd.EventBus.Dapr; using Dapr.Client; +using MediatR; +using System.Reflection; namespace Microsoft.Extensions.DependencyInjection; @@ -14,12 +16,23 @@ public static class DaprEventBusServiceCollectionExtensions /// /// /// The app name used when publishing integration events. + /// Assemblies to scan by MediatR /// - public static IServiceCollection AddDaprEventBus(this IServiceCollection services, string appName) + public static IServiceCollection AddDaprEventBus( + this IServiceCollection services, + string appName, + params Assembly[] assemblies) { services.Configure(o => o.AppName = appName); services.AddControllers().AddDapr(); services.AddScoped(); + + if (assemblies.Length > 0) + { + services.AddMediatR(assemblies); + } + return services; } + } diff --git a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs index ba8f983..5ba5c67 100644 --- a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs +++ b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs @@ -9,11 +9,9 @@ var builder = WebApplication.CreateBuilder(args); -builder.Services.AddCqrs( - Assembly.GetExecutingAssembly(), - typeof(TestIntegrationEvent).Assembly) - .AddDefaultDateTimeAndRandomProvider(); -builder.Services.AddDaprEventBus(Constants.AppName); +builder.Services.AddCqrs(Assembly.GetExecutingAssembly(), typeof(TestIntegrationEvent).Assembly) + .AddDefaultDateTimeAndRandomProvider() + .AddDaprEventBus(Constants.AppName); builder.Services.AddControllers().AddCqrsModelBinderProvider(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle diff --git a/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs b/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs index 51c7e66..e462641 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs +++ b/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs @@ -7,27 +7,29 @@ using Cnblogs.Architecture.TestIntegrationEvents; using FluentAssertions; using MediatR; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; namespace Cnblogs.Architecture.IntegrationTests; -[Collection(IntegrationTestCollection.Name)] public class IntegrationEventHandlerTests { - private readonly IntegrationTestFactory _factory; - - public IntegrationEventHandlerTests(IntegrationTestFactory factory) - { - _factory = factory; - } - [Fact] public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync() { // Arrange - var client = _factory.CreateClient(); + var builder = WebApplication.CreateBuilder(); + builder.Services + .AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly) + .AddHttpContextAccessor(); + builder.WebHost.UseTestServer(); + var app = builder.Build(); + app.Subscribe(); + await app.StartAsync(); + var client = app.GetTestClient(); // Act var subscriptions = await client.GetFromJsonAsync("/dapr/subscribe");