1

To configure a rabbitmq consumer in .net core I see examples of Console Application. I am not sure for production ready apps can a Console app would be a right fit as stand alone Rabbitmq Consumer. Please share your thoughts what's the recommended way of building a consumer and deploy to production. And also read that not to configure Consumer in ASP.net core apps as IIS could recycle and not a good choice

MADHU VS
  • 79
  • 7
  • 1
    1) There are many things factually incorrect with what you're saying. 2) I'm not sure what - if any - specific question you're asking. 3) If you actually have a question, I;m not sure SO is the best forum. – paulsm4 Mar 29 '19 at 04:51
  • We dont share thoughts on SO, we share facts. Ask a question, get an answer :-) – Robert Perry Mar 29 '19 at 08:01
  • Please see this link : https://github.com/EasyNetQ/EasyNetQ/wiki/Quick-Start – Amin Golmahalleh Sep 03 '19 at 06:25

4 Answers4

2

if you want to pub/sub in .net core

first you need install lib RabbitMQ.Client

Installing NuGet package RabbitMQ.Client

then you can use RabbitMQ in .net core same Console Application.

You can see examples of the .net core 2.2 here.

Publish

public bool Publish()
{
    var RoutingKey = "QueueName";
    var Body = "Message";
    var Factory = new ConnectionFactory
        {
            UserName = "guest",
            Password = "guest",
            Port = 5672,
            HostName = "localhost",
            VirtualHost = "/"
        };

        using (var connection = Factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare("messageexchange", ExchangeType.Direct);
            channel.QueueDeclare(RoutingKey, true, false, false, null);
            channel.QueueBind(RoutingKey, "messageexchange", RoutingKey, null);
            channel.BasicPublish("messageexchange", RoutingKey, null, Encoding.UTF8.GetBytes(Body));
        }

        return true;
        }

Subscribe

 public SubscribeResult Subscribe()
    {
        var Queue = "QueueName";
        var Factory = new ConnectionFactory
        {
            UserName = "guest",
            Password = "guest",
            Port = 5672,
            HostName = "localhost",
            VirtualHost = "/"
        };
        var subscribeResult = new SubscribeResult(); ;
        using (var connection = Factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {

            channel.QueueDeclare(queue: Queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
            var result = channel.BasicGet(queue: Queue, autoAck: true);
            if (result != null)
            {
                subscribeResult.Body = Encoding.UTF8.GetString(result.Body);
            }
        }
        return subscribeResult;
    }
Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34
1

We're using RabbitMQ in a large .NET Core solution split into microservices. Each microservice is just a regular console app or ASP.NET Core, which then can be run on Windows as a service or as a daemon on Linux.

Starting from .NET Core 2.1 Microsoft introduced Microsoft.AspNetCore.Hosting.WindowsServices namespace allowing to run ASP.NET Core as a Windows service: https://learn.microsoft.com/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.2&tabs=visual-studio

or you can use ServiceBase from Microsoft.Windows.Compatibility for console apps: https://www.pmichaels.net/2019/01/08/creating-a-windows-service-using-net-core-2-2/

We're deploying mostly to Linux systems, so we use an approach of HostedServices:

static async Task Main(string[] args)
        {
            await new HostBuilder()
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<MyService>();
                })
                .RunConsoleAsync();
        }
public sealed class MyService : IHostedService, IDisposable
{
  public Task StartAsync(CancellationToken cancellationToken) {}
  public Task StopAsync(CancellationToken cancellationToken)
}
0

You can host consumers in a Windows service, or a console app (which is basically what a service is, but with some start/stop behavior to interact with the Windows service control manager).

For Windows, you can use Topshelf. For Linux or Mac, you just use a Console app. And future versions of .NET core will likely integrate the service controls when you can use any console application as a service with a single statement in the configuration.

You can see examples of the console service here and there are many examples of using Topshelf for Windows.

Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
0

**

class Program
{
    static void Main(string[] args)
    {
        var connectionFactory = new ConnectionFactory()
        {
            UserName = "guest",
            Password = "guest",
            AutomaticRecoveryEnabled = true,
            HostName = "localhost",
            Port = 5672,
            DispatchConsumersAsync = true
        };

        var connection = connectionFactory.CreateConnection();
        //services.AddSingleton(connection);

        //services.AddScoped(serviceProvider =>
        //{
        //    var conn = serviceProvider.GetService<IConnection>();
        //    return conn.CreateModel();
        //});

        var channel = connection.CreateModel();

        var basicConsumer = new AsyncEventingBasicConsumer(channel);
        basicConsumer.Received += BasicConsumerOnReceived;
        channel.QueueDeclare("queename", true, false, false, null);
        //publish test message
        //channel.BasicPublish("", "queename", false, null, Encoding.UTF8.GetBytes(" test message"));
        channel.BasicConsume("queename", true, basicConsumer);
        System.Console.ReadLine();
    }


    private static Task BasicConsumerOnReceived(object sender, BasicDeliverEventArgs args)
    {
        var body = args.Body;
        var str = Encoding.UTF8.GetString(body.ToArray());
        System.Console.WriteLine(str);      
        return Task.CompletedTask;
    }
}

**

  • With the code I shared in .Net Core, you can listen to the rabbit queue without applying the console. Comment sections can be added to the service. – Marko Asmann Dec 07 '21 at 23:10