Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: check if IEventBus is registered #7

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
cnblogs-dudu marked this conversation as resolved.
Show resolved Hide resolved
if (!serviceCheck.IsService(typeof(IEventBus)))
{
throw new InvalidOperationException(
$"{nameof(IEventBus)} has not been registered. Please using AddDaprEventBus to register.");
cnblogs-dudu marked this conversation as resolved.
Show resolved Hide resolved
}

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>();
}
}