Open Closed

DistributedEventBus and RabbitMQ Event Handling Issue in MVC #9110


User avatar
0
IbrahimSarigoz created

Hi,
I am trying to trigger an event in my project using DistributedEventBus and RabbitMQ, but I cannot catch the event on the Web side. I would like to explain what I have set up in each layer so you can help me understand if I'm missing something.

My project is MVC and Non-Tiered.

Application.Contracts Layer:

I have created a simple ETO (Event Transfer Object):


[EventName("RecurringJobCreatedOrUpdatedEto")]
public class RecurringJobCreatedOrUpdatedEto
{
    public int JobId { get; set; }
    public string JobName { get; set; }
}

Application Layer:

In my AppService, I am publishing the event:

private readonly IDistributedEventBus _distributedEventBus;

public RecurringJobsAppServiceBase(IDistributedEventBus distributedEventBus)
{
    _distributedEventBus = distributedEventBus;
}

public virtual async Task UpdateAsync(int id, RecurringJobUpdateDto input)
{
    var recurringJob = await _recurringJobManager.UpdateAsync(
        id,
        input.JobName, input.IsActive, input.CronExpression, input.ConcurrencyStamp
    );

    var eventData = new RecurringJobCreatedOrUpdatedEto
    {
        JobId = id,
        JobName = input.JobName,
    };

    await _distributedEventBus.PublishAsync(eventData);

    return ObjectMapper.Map(recurringJob);
}

I did not make any changes to the ApplicationModule.

Web Layer:

I created a handler for the event:


public class RecurringJobCreatedOrUpdatedEventHandler : IDistributedEventHandler, ITransientDependency
{
    private readonly ILogger _logger;

    public RecurringJobCreatedOrUpdatedEventHandler(ILogger logger)
    {
        _logger = logger;
    }

    public Task HandleEventAsync(RecurringJobCreatedOrUpdatedEto eventData)
    {
        _logger.LogInformation($"Event! JobId: {eventData.JobId}, JobName: {eventData.JobName}");
        return Task.CompletedTask;
    }
}

Inside WebModule, I did the following:

Added typeof(AbpEventBusRabbitMqModule) to the dependencies.

In ConfigureServices, I wrote:

context.Services.AddAssemblyOf();

Configure(options =>
{
    options.AutoEventSelectors.AddAll();
});

appsettings.json Configuration:


"EventBus": {
    "UseRabbitMq": true
},
"RabbitMQ": {
    "Connections": {
        "Default": {
            "HostName": "localhost",
            "UserName": "myuser",
            "Password": "1q2w3E*",
            "Port": "5672"
        }
    },
    "EventBus": {
        "ClientName": "TEST_Web",
        "ExchangeName": "TESTExchange"
    }
}

I am sure my rabbitmq is working. when i publish exchange is changing

image.png,

Problem:
The event is successfully published, but the handler in the Web layer is not triggered, and the event is not being caught.

Thank you in advance for your help!


9 Answer(s)
  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hi,

    If you're implementing a distributed event handler like this:

    public class RecurringJobCreatedOrUpdatedEventHandler 
        : IDistributedEventHandler, ITransientDependency
    
    

    It won’t work because IDistributedEventHandler must be generic. You need to specify the event type it handles, like this:

    public class RecurringJobCreatedOrUpdatedEventHandler 
        : IDistributedEventHandler<RecurringJobCreatedOrUpdatedEto>, ITransientDependency
    
    

    Also, make sure your Web module depends on AbpEventBusRabbitMqModule. Otherwise, the event bus infrastructure won't be properly initialized, and your event handler won’t get triggered even if the event is published successfully. See: https://abp.io/docs/latest/framework/infrastructure/event-bus/distributed/rabbitmq#installation

  • User Avatar
    0
    IbrahimSarigoz created

    Hi, I made a copy-paste mistake.
    My handler was actually:

    public class RecurringJobCreatedOrUpdatedEventHandler : IDistributedEventHandler<RecurringJobCreatedOrUpdatedEto>, ITransientDependency
    {
    private readonly ILogger<RecurringJobCreatedOrUpdatedEventHandler> _logger;

    public RecurringJobCreatedOrUpdatedEventHandler(ILogger<RecurringJobCreatedOrUpdatedEventHandler> logger)
    {
        _logger = logger;
    }
    
    public Task HandleEventAsync(RecurringJobCreatedOrUpdatedEto eventData)
    {
        _logger.LogInformation($"Event! JobId: {eventData.JobId}, JobName: {eventData.JobName}");
        return Task.CompletedTask;
    }
    

    }

    So the problem still persists.

  • User Avatar
    0
    IbrahimSarigoz created

    I tried the same things in a new test project, and they worked correctly. Since I created the test project with version 8.2, there were no issues. However, the project I am currently working on has been upgraded from .NET 6.
    In your opinion, what could I be missing at this point?

  • User Avatar
    0
    IbrahimSarigoz created

    Is there anyone who can help please?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi IbrahimSarigoz

    Can you share a test project that can reproduce the problem?

    liming.ma@volosoft.com
    Thanks

  • User Avatar
    0
    IbrahimSarigoz created

    Hi maliming,

    It works in my test project, but not in my existing project that has been developed over time. I’m unable to share the main project itself, but I can provide the module configurations and other relevant settings.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    but I can provide the module configurations and other relevant settings.

    You can share a project so that I can reproduce the problem. Then I will know what is wrong.

    Otherwise, it will be difficult for me to troubleshoot the problem.

    Thanks.

  • User Avatar
    0
    IbrahimSarigoz created

    Hi,

    I finally found my mistake — it was all about using the correct namespace in the handler class.
    I was mistakenly using 'using Volo.Dependency;' instead of 'using Volo.Abp.DependencyInjection;'.
    It didn’t throw any errors, but it wasn’t working either. :)

    If possible, could you please refund my ticket?

    Thanks

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    If possible, could you please refund my ticket?

    This question does not meet the conditions for a refund. Thank you for your understanding.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on April 16, 2025, 12:13