Skip to content

Commit

Permalink
refactor: check if IEventBus is registered
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 6, 2023
1 parent 1b6bf64 commit 9359606
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -62,6 +63,14 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
where TEvent : IntegrationEvent
{
EnsureDaprSubscribeHandlerMapped(builder);

var serviceCheck = builder.ServiceProvider.GetRequiredService<IServiceProviderIsService>();
if (!serviceCheck.IsService(typeof(IEventBus)))
{
throw new InvalidOperationException(
$"{nameof(IEventBus)} has not been registered. Please using AddDaprEventBus to register.");
}

var result = builder
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
.WithTopic(DaprOptions.PubSubName, DaprUtils.GetDaprTopicName<TEvent>(appName));
Expand Down
2 changes: 0 additions & 2 deletions test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection;
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Cnblogs.Architecture.IntegrationTestProject.Application.Commands;
using Cnblogs.Architecture.IntegrationTestProject.Application.Queries;
using Cnblogs.Architecture.IntegrationTestProject.Payloads;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;

[assembly:AssemblyAppName("test")]
[assembly: AssemblyAppName("test")]
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr.csproj" />
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.EntityFramework\Cnblogs.Architecture.Ddd.Cqrs.EntityFramework.csproj" />
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.MongoDb\Cnblogs.Architecture.Ddd.Cqrs.MongoDb.csproj" />
<ProjectReference Include="..\Cnblogs.Architecture.TestIntegrationEvents\Cnblogs.Architecture.TestIntegrationEvents.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Cnblogs.Architecture.TestIntegrationEvents;

using FluentAssertions;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace Cnblogs.Architecture.UnitTests.EventBus;

Expand All @@ -14,6 +13,7 @@ public void SubscribeByAssemblyMeta_Success()
{
// Arrange
var builder = WebApplication.CreateBuilder();
builder.Services.AddDaprEventBus(nameof(AssemblyAttributeTests));
var app = builder.Build();

// Act
Expand All @@ -22,4 +22,18 @@ public void SubscribeByAssemblyMeta_Success()
// Assert
act.Should().NotThrow();
}

[Fact]
public void SubscribeByAssemblyMeta_Throw()
{
// Arrange
var builder = WebApplication.CreateBuilder();
var app = builder.Build();

// Act
var act = () => app.Subscribe<TestIntegrationEvent>();

// Assert
act.Should().Throw<InvalidOperationException>();
}
}

0 comments on commit 9359606

Please sign in to comment.