1

enter image description hereMy question is simple. I wanna use rabbitmq to make message queue by using asp.net webapi. On the other hand, GetAllQueues not returning any message. Message is always null. Below code perfect in console application but message always null when i run below post :

http://localhost:53301/api/CustomerPipline/?queueName=test123

enter image description here


using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RabbitMQ.Client;
using System.Text;
using RabbitMQ.Client.Events;
using System.Diagnostics;
using RabbitMQ.Client.Exceptions;

namespace Atom.Mqpipline.Controllers
{
    public class CustomerPiplineController : ApiController
    {
        private static readonly string _queueName = "test123";


        [HttpPost]
        public HttpResponseMessage AddQueue(int customerId)
        {

           var  publisher = new Publisher(_queueName, "Hello RabbitMQ World!");
            var resp = Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject("OK"));

            return resp;
        }
        [HttpGet]
        public HttpResponseMessage GetAllQueues(string queueName)
        {
            var consumer = new Consumer(queueName);
            var resp = Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(consumer.Message));
            return resp;
        }
    }

}

public class RabbitMQService
{
    private readonly string _hostName = "localhost";

    public IConnection GetRabbitMQConnection()
    {
        ConnectionFactory connectionFactory = new ConnectionFactory()
        {
            HostName = _hostName
        };

        return connectionFactory.CreateConnection();
    }
}
public class Publisher
{
    private readonly RabbitMQService _rabbitMQService;

    public Publisher(string queueName, string message)
    {
        _rabbitMQService = new RabbitMQService();

        using (var connection = _rabbitMQService.GetRabbitMQConnection())
        {
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queueName, false, false, false, null);
                channel.BasicPublish("", queueName, null, Encoding.UTF8.GetBytes(message));
            }
        }
    }
}

public class Consumer
{
    private readonly RabbitMQService _rabbitMQService;
    public string Message { get; set; }
    public Consumer(string queueName)
    {

        _rabbitMQService = new RabbitMQService();

        using (var connection = _rabbitMQService.GetRabbitMQConnection())
        {
            using (var channel = connection.CreateModel())
            {
                var consumer = new EventingBasicConsumer(channel);
                // Received event'i sürekli listen modunda olacaktır.
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Debug.WriteLine(message);
                    Message = message;
                };

                channel.BasicConsume(queueName, true, consumer);
            }
        }
    }
}


2 Answers 2

2

you cannot call consumer. consumer action runs when received a message not when you call.

  • create only one instance of Consumer on your app..
  • add a queue received message. or push message direct to client via SignalR.
Sign up to request clarification or add additional context in comments.

1 Comment

can you please eleborate or give real ipmlementation "crete only one instance of Consumer on your app"
1

I don't think you can consume message in a postback or a rest endpoint. But you can start a Task that receive the queue on the startup of your project.

        public void Configure(IApplicationBuilder app)
        {
            [...]
            Task.Run(GetAllQueues(queueName));
        }
        public void GetAllQueues(string queueName)
        {
            var consumer = new Consumer(queueName);
            var resp = Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(consumer.Message));
            return resp;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.