Skip to content

Commit

Permalink
Merge pull request #16 from cnblogs/register-dapreventbus-with-event-…
Browse files Browse the repository at this point in the history
…handlers

feat: register DaprEventBus with integration event handlers
ikesnowy authored Feb 8, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 5583eab + 1ac0e35 commit 7e43a23
Showing 3 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Register <see cref="DaprClient"/> and <see cref="IEventBus"/>.
/// Register <see cref="DaprClient"/> and <see cref="IEventBus"/>.
/// The alternative is using services.AddCqrs().AddDaprEventBus() in <see cref="CqrsInjectorExtensions"/>.
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/></param>
/// <param name="appName">The app name used when publishing integration events.</param>
/// <param name="assemblies">Assemblies to scan by MediatR</param>
/// <returns></returns>
public static IServiceCollection AddDaprEventBus(this IServiceCollection services, string appName)
public static IServiceCollection AddDaprEventBus(
this IServiceCollection services,
string appName,
params Assembly[] assemblies)
{
services.Configure<DaprOptions>(o => o.AppName = appName);
services.AddControllers().AddDapr();
services.AddScoped<IEventBus, DaprEventBus>();

if (assemblies.Length > 0)
{
services.AddMediatR(assemblies);
}

return services;
}

}
8 changes: 3 additions & 5 deletions test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
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;
}

[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<TestIntegrationEvent>();
await app.StartAsync();
var client = app.GetTestClient();
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!");

// Act

0 comments on commit 7e43a23

Please sign in to comment.