Home > Software engineering >  Consumer not consuming message Service Bus Queue
Consumer not consuming message Service Bus Queue

Time:09-22

I am trying Service bus queue for my knowledge recently in ASP.NET Core 5.0 where I have created Producer webapi and a consumer web API and using masstransite. But due to some reason I am not able to consume the message.

Message Producer Code controller:

    [HttpGet]
    [Route("sender")]
    public async Task SendUsingAzureServiceBusQeue()
    {
        try
        {
            var sendEndpoint =
            await _serviceEndPointProvider.GetSendEndpoint(
                new Uri("myservicebusurl/test"));
            await sendEndpoint.Send<Test>(new Test { message = "Hi This message from sender" });
        }
        catch(Exception ex)
        {

        }
    }

Producer Startup Class Settings:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMassTransit(x =>
        {
            x.UsingAzureServiceBus((context, cfg) =>
            {
                cfg.Host("Endpoint=myservicebusconnectionstring");
            });
        });

        services.AddMassTransitHostedService();

        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "AKS Example", Version = "v1" });
        });
    }

Consumer Code Startup class:

 public void ConfigureServices(IServiceCollection services)
    {
       
        services.AddMassTransit(x =>
            {
                
                x.UsingAzureServiceBus((context, cfg) =>
                {
                    cfg.Host("Endpoint=myservicebusconnectionstring");
                    cfg.ReceiveEndpoint("test", endpoint =>
                    {
                        endpoint.ConfigureConsumer<MessageConsumer>(context);
                    });
                });
                x.AddConsumer<MessageConsumer>();
            });
        services.AddMassTransitHostedService(true);
        services.AddControllers();
    }

Consumer Code Consumer Class:

 public class MessageConsumer : IConsumer<Test>
{
    public Task Consume(ConsumeContext<Test> context)
    {
        Console.WriteLine($"Message processed: Message:{context.Message.message}");
        return Task.CompletedTask;
    }
}

I am able to see message are being pushed but not received on consumer side. Also I noticed there is a Topic being created though I am using queue only name "test" for publish and subscribe.

CodePudding user response:

Ensure that both projects have the same message type, including namespace as pointed out in the documentation.

  • Related