diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/ApiControllerBase.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/ApiControllerBase.cs index 78fbe03..2d11543 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/ApiControllerBase.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/ApiControllerBase.cs @@ -52,7 +52,7 @@ protected IActionResult HandleCommandResponse(CommandResponse { if (response.IsSuccess()) { - return Request.Headers.CqrsVersion() > 1 ? Ok(response) : Ok(response.Response); + return Request.Headers.CqrsVersion() > 1 ? new CqrsObjectResult(response) : Ok(response.Response); } return HandleCommandResponse((CommandResponse)response); diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection/ServiceCollectionInjector.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection/ServiceCollectionInjector.cs index 6db17e7..64b92af 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection/ServiceCollectionInjector.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection/ServiceCollectionInjector.cs @@ -1,5 +1,4 @@ using System.Reflection; - using Cnblogs.Architecture.Ddd.Cqrs.Abstractions; using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection; using MediatR; @@ -22,6 +21,12 @@ public static CqrsInjector AddCqrs(this IServiceCollection services, params Asse { services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>)); + if (assemblies.Length == 0) + { + // mediator needs at least one assembly to inject from + assemblies = [typeof(CqrsInjector).Assembly]; + } + services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(assemblies)); return new CqrsInjector(services); } diff --git a/test/Cnblogs.Architecture.IntegrationTests/CommandResponseHandlerTests.cs b/test/Cnblogs.Architecture.IntegrationTests/CommandResponseHandlerTests.cs index 145eb5b..28cea2e 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/CommandResponseHandlerTests.cs +++ b/test/Cnblogs.Architecture.IntegrationTests/CommandResponseHandlerTests.cs @@ -53,6 +53,7 @@ public async Task MinimalApi_CqrsV2_CommandResponseAsync() var content = await response.Content.ReadFromJsonAsync>(); // Assert + response.Headers.CqrsVersion().Should().BeGreaterThan(1); content.Should().NotBeNull(); content!.Response.Should().NotBeNullOrEmpty(); } @@ -89,7 +90,8 @@ public async Task Mvc_CurrentCqrsVersion_CommandResponseAsync() // Assert response.Should().BeSuccessful(); - content!.Response.Should().NotBeNull(); + response.Headers.CqrsVersion().Should().BeGreaterThan(1); + content!.Response.Should().NotBeNullOrEmpty(); } [Theory] diff --git a/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs b/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs index a017ad5..8930ffd 100644 --- a/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs +++ b/test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs @@ -21,7 +21,7 @@ public async Task Dapr_SubscribeEndpoint_OkAsync(SubscribeType subscribeType) { // Arrange var builder = WebApplication.CreateBuilder(); - builder.Services.AddEventBus(o => o.UseDapr(nameof(DaprTests))); + builder.Services.AddCqrs(typeof(TestIntegrationEvent).Assembly).AddEventBus(o => o.UseDapr(nameof(DaprTests))); builder.WebHost.UseTestServer(); await using var app = builder.Build(); @@ -53,7 +53,7 @@ public async Task Dapr_SubscribeWithoutAnyAssembly_OkAsync() { // Arrange var builder = WebApplication.CreateBuilder(); - builder.Services.AddEventBus(o => o.UseDapr(nameof(DaprTests))); + builder.Services.AddCqrs().AddEventBus(o => o.UseDapr(nameof(DaprTests))); builder.WebHost.UseTestServer(); var app = builder.Build(); diff --git a/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs b/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs index 3ad6a62..47325a3 100644 --- a/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs +++ b/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs @@ -3,6 +3,7 @@ using Cnblogs.Architecture.TestIntegrationEvents; using FluentAssertions; using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; namespace Cnblogs.Architecture.UnitTests.EventBus; @@ -13,7 +14,7 @@ public void SubscribeByAssemblyMeta_Success() { // Arrange var builder = WebApplication.CreateBuilder(); - builder.Services.AddEventBus(o => o.UseDapr(nameof(AssemblyAttributeTests))); + builder.Services.AddCqrs().AddEventBus(o => o.UseDapr(nameof(AssemblyAttributeTests))); var app = builder.Build(); // Act