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

Do not auto discover generic event handlers #17465

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ private static void AddEventHandlers(IServiceCollection services)

services.OnRegistered(context =>
{
if (context.ImplementationType.IsGenericTypeDefinition)
{
return;
}

if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(ILocalEventHandler<>)))
{
localHandlers.Add(context.ImplementationType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;

namespace Volo.Abp.EventBus.Distributed;

public class MyGenericDistributedEventHandler<TEvent> : IDistributedEventHandler<TEvent>
{
private readonly ICurrentTenant _currentTenant;

public MyGenericDistributedEventHandler(ICurrentTenant currentTenant)
{
_currentTenant = currentTenant;
}

public static Guid? TenantId { get; set; }

public Task HandleEventAsync(TEvent eventData)
{
TenantId = _currentTenant.Id;
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Modularity;

namespace Volo.Abp.EventBus;

[DependsOn(typeof(AbpEventBusModule))]
public class EventBusTestModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.TryAddTransient(typeof(MyGenericDistributedEventHandler<>));
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var distributedEventBus = context.ServiceProvider.GetRequiredService<IDistributedEventBus>();
var scopeFactory = context.ServiceProvider.GetRequiredService<IServiceScopeFactory>();

var eventType = typeof(MySimpleEventData);
var myHandlerType = typeof(MyGenericDistributedEventHandler<>).MakeGenericType(eventType);
distributedEventBus.Subscribe(eventType, new IocEventHandlerFactory(scopeFactory, myHandlerType));
maliming marked this conversation as resolved.
Show resolved Hide resolved
}
}