Rebus publisher tries to route the message

122 Views Asked by At

I feel like I didn't fully understand how Rebus works...

I have a system where an API should queue messages in a SQL table, and a Worker Service that will do the processing of the messages. I noticed that the publisher tries to route the messages to a handler and if it does not find a proper handler it raises an exception and moves the message to the error queue... I just want my API to queue the message and let the Worker Service to get the message when ready...

My API configuration is:

services.AddRebus((configure) => configure
                .Logging(l => l.NLog())
                .Transport(t => t.UseSqlServer(transportOptions, "QueueMsgs"))
                .Options(o => o.SetNumberOfWorkers(1))
                .Options(o => o.SimpleRetryStrategy(maxDeliveryAttempts: 10))               
            );

And I try to send messages like:

await _bus.Send(user);

If I debug the Worker Service at the same time, everything works perfectly, but my goal is to not need the Worker Service to be active to keep the messages in the queue...

What can I do?

I tried to use publish to queue the message

await _bus.Publish(user);

And adding the Subscription:

services.AddRebus((configure) => configure
                .Logging(l => l.NLog())
                .Transport(t => t.UseSqlServer(transportOptions, "QueueMsgs"))
                .Options(o => o.SetNumberOfWorkers(1))
                .Options(o => o.SimpleRetryStrategy(maxDeliveryAttempts: 10))
                .Subscriptions(s => s.StoreInSqlServer(sqlConnectionString, "QueueMsgsSubs"))               
            //.Options(o => )
            );

I'm quite lost, to be honest.

Please any help will be highly appreciated. Thank you!!

1

There are 1 best solutions below

0
Luis Agudo On

As I suspected, the fault was on me, I didn't have the correct configuration:

This is for the service configuration:

public static IServiceCollection AddRebusInjection(this IServiceCollection services, string sqlConnectionString)
{
    string queueTable = "QueueMsgs";
    services.AddRebus((configure) => configure
        .Logging(l => l.NLog())
        .Transport(t => t.UseSqlServerAsOneWayClient(sqlConnectionString))
        .Options(o => o.SimpleRetryStrategy(maxDeliveryAttempts: 10, errorQueueAddress: "queueErrors"))
        .Routing(r => r.TypeBased()
            .Map<UserDTO>(queueTable)
            .Map<StudyRoleDTO>(queueTable))
    );


    return services;
}

And we need to send messages with .Send:

[HttpPost, Route("RegisterAdminUser")]
        public async Task<IActionResult> SetAdminUser(SetAdminUserRequest request)
        {
            _logger.LogTrace($"Received a SetAdminUser request: {JsonConvert.SerializeObject(request, Formatting.None)}");
            var user = new UserDTO()
            {
                Name = request.LastName +", "+request.FirstName,
                FirstName = request.FirstName,
                LastName = request.LastName,
                Email = request.Email,
                UserGUID = request.UserGuid,
                AdminType = await _userService.GetAdminTypeAsync(request.Roles)
            };
            await _userService.CheckUserAsync(user);

            await _bus.Send(user);

            return Accepted();
        }

And that's it. Now my messages are nice and comfy waiting for the consumer to get them :)