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..f245f9d 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,9 @@ -using Cnblogs.Architecture.Ddd.EventBus.Abstractions; +using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr; +using Cnblogs.Architecture.Ddd.EventBus.Abstractions; using Cnblogs.Architecture.Ddd.EventBus.Dapr; using Dapr.Client; +using MediatR; +using System.Reflection; namespace Microsoft.Extensions.DependencyInjection; @@ -10,16 +13,28 @@ namespace Microsoft.Extensions.DependencyInjection; public static class DaprEventBusServiceCollectionExtensions { /// - /// Register and . + /// Register and . + /// The alternative is using services.AddCqrs().AddDaprEventBus() in . /// /// /// 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 4087e33..e8b2bfe 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs +++ b/test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs @@ -1,20 +1,24 @@ using System.Net.Http.Json; using Cnblogs.Architecture.IntegrationTestProject; +using Cnblogs.Architecture.IntegrationTestProject.EventHandlers; 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; using Xunit.Abstractions; namespace Cnblogs.Architecture.IntegrationTests; -[Collection(IntegrationTestCollection.Name)] public class IntegrationEventHandlerTests { - private readonly IntegrationTestFactory _factory; private readonly ITestOutputHelper _testOutputHelper; - public IntegrationEventHandlerTests(IntegrationTestFactory factory, ITestOutputHelper testOutputHelper) + public IntegrationEventHandlerTests(ITestOutputHelper testOutputHelper) { - _factory = factory; _testOutputHelper = testOutputHelper; } @@ -22,7 +26,15 @@ public IntegrationEventHandlerTests(IntegrationTestFactory factory, ITestOutputH 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(); var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!"); // Act