Skip to content

Commit 1ac0e35

Browse files
committed
feat: register DaprEventBus with integration event handlers
1 parent 5583eab commit 1ac0e35

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
1+
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
2+
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
23
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
34
using Dapr.Client;
5+
using MediatR;
6+
using System.Reflection;
47

58
namespace Microsoft.Extensions.DependencyInjection;
69

@@ -10,16 +13,28 @@ namespace Microsoft.Extensions.DependencyInjection;
1013
public static class DaprEventBusServiceCollectionExtensions
1114
{
1215
/// <summary>
13-
/// Register <see cref="DaprClient"/> and <see cref="IEventBus"/>.
16+
/// Register <see cref="DaprClient"/> and <see cref="IEventBus"/>.
17+
/// The alternative is using services.AddCqrs().AddDaprEventBus() in <see cref="CqrsInjectorExtensions"/>.
1418
/// </summary>
1519
/// <param name="services"><see cref="IServiceCollection"/></param>
1620
/// <param name="appName">The app name used when publishing integration events.</param>
21+
/// <param name="assemblies">Assemblies to scan by MediatR</param>
1722
/// <returns></returns>
18-
public static IServiceCollection AddDaprEventBus(this IServiceCollection services, string appName)
23+
public static IServiceCollection AddDaprEventBus(
24+
this IServiceCollection services,
25+
string appName,
26+
params Assembly[] assemblies)
1927
{
2028
services.Configure<DaprOptions>(o => o.AppName = appName);
2129
services.AddControllers().AddDapr();
2230
services.AddScoped<IEventBus, DaprEventBus>();
31+
32+
if (assemblies.Length > 0)
33+
{
34+
services.AddMediatR(assemblies);
35+
}
36+
2337
return services;
2438
}
39+
2540
}

test/Cnblogs.Architecture.IntegrationTestProject/Program.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99

1010
var builder = WebApplication.CreateBuilder(args);
1111

12-
builder.Services.AddCqrs(
13-
Assembly.GetExecutingAssembly(),
14-
typeof(TestIntegrationEvent).Assembly)
15-
.AddDefaultDateTimeAndRandomProvider();
16-
builder.Services.AddDaprEventBus(Constants.AppName);
12+
builder.Services.AddCqrs(Assembly.GetExecutingAssembly(), typeof(TestIntegrationEvent).Assembly)
13+
.AddDefaultDateTimeAndRandomProvider()
14+
.AddDaprEventBus(Constants.AppName);
1715
builder.Services.AddControllers().AddCqrsModelBinderProvider();
1816

1917
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
using System.Net.Http.Json;
22
using Cnblogs.Architecture.IntegrationTestProject;
3+
using Cnblogs.Architecture.IntegrationTestProject.EventHandlers;
34
using Cnblogs.Architecture.TestIntegrationEvents;
45
using FluentAssertions;
6+
using MediatR;
7+
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Mvc.Testing;
10+
using Microsoft.AspNetCore.TestHost;
11+
using Microsoft.Extensions.DependencyInjection;
512
using Xunit.Abstractions;
613

714
namespace Cnblogs.Architecture.IntegrationTests;
815

9-
[Collection(IntegrationTestCollection.Name)]
1016
public class IntegrationEventHandlerTests
1117
{
12-
private readonly IntegrationTestFactory _factory;
1318
private readonly ITestOutputHelper _testOutputHelper;
1419

15-
public IntegrationEventHandlerTests(IntegrationTestFactory factory, ITestOutputHelper testOutputHelper)
20+
public IntegrationEventHandlerTests(ITestOutputHelper testOutputHelper)
1621
{
17-
_factory = factory;
1822
_testOutputHelper = testOutputHelper;
1923
}
2024

2125
[Fact]
2226
public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync()
2327
{
2428
// Arrange
25-
var client = _factory.CreateClient();
29+
var builder = WebApplication.CreateBuilder();
30+
builder.Services
31+
.AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly)
32+
.AddHttpContextAccessor();
33+
builder.WebHost.UseTestServer();
34+
var app = builder.Build();
35+
app.Subscribe<TestIntegrationEvent>();
36+
await app.StartAsync();
37+
var client = app.GetTestClient();
2638
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!");
2739

2840
// Act

0 commit comments

Comments
 (0)