Skip to content

Commit

Permalink
Merge pull request #7 from cnblogs/check-if-ieventbus-is-registered
Browse files Browse the repository at this point in the history
refactor: check if IEventBus is registered
  • Loading branch information
ikesnowy authored Feb 6, 2023
2 parents 1b6bf64 + 5270c2b commit b81706c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class DaprOptions
/// <summary>
/// 是否调用过 <c>app.MapSubscribeHandler()</c>
/// </summary>
internal static bool IsDaprSubscribeHandlerMapped { get; set; }
internal bool IsDaprSubscribeHandlerMapped { get; set; }

internal bool IsEventBusRegistered { get; set; }
}
30 changes: 26 additions & 4 deletions src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -61,7 +63,10 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
string appName)
where TEvent : IntegrationEvent
{
EnsureDaprSubscribeHandlerMapped(builder);
var daprOptions = builder.ServiceProvider.GetRequiredService<IOptions<DaprOptions>>().Value;
EnsureDaprSubscribeHandlerMapped(builder, daprOptions);
EnsureEventBusRegistered(builder, daprOptions);

var result = builder
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
.WithTopic(DaprOptions.PubSubName, DaprUtils.GetDaprTopicName<TEvent>(appName));
Expand Down Expand Up @@ -95,9 +100,26 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
}
}

private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder)
private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, DaprOptions daprOptions)
{
if (daprOptions.IsEventBusRegistered)
{
return;
}

var serviceCheck = builder.ServiceProvider.GetRequiredService<IServiceProviderIsService>();
if (!serviceCheck.IsService(typeof(IEventBus)))
{
throw new InvalidOperationException(
$"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?");
}

daprOptions.IsEventBusRegistered = true;
}

private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder, DaprOptions daprOptions)
{
if (DaprOptions.IsDaprSubscribeHandlerMapped)
if (daprOptions.IsDaprSubscribeHandlerMapped)
{
return;
}
Expand All @@ -108,6 +130,6 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
}

builder.MapSubscribeHandler();
DaprOptions.IsDaprSubscribeHandlerMapped = true;
daprOptions.IsDaprSubscribeHandlerMapped = true;
}
}
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 b81706c

Please sign in to comment.