Skip to content
Atallah0 edited this page Jul 14, 2024 · 22 revisions

EasyNetQ Quick Start Guide

Welcome to EasyNetQ. This guide shows you how to get up and running with EasyNetQ in about 10 minutes.

You can find the code for this Quick Start on GitHub here: EasyNetQ Samples.

EasyNetQ is a simple-to-use client API for RabbitMQ.

Setup

  1. Run RabbitMQ Docker Container:

    docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
  2. Navigate to RabbitMQ Management URL:

    http://localhost:15672/
    

    RabbitMQ management UI

Create Solution and Projects

  1. Create a new solution named EasyNetQTest with three C# projects:

    • Messages (Class Library)
    • Publisher (Console Application)
    • Subscriber (Console Application)
  2. Install EasyNetQ:

    dotnet add Publisher package EasyNetQ 
    dotnet add Subscriber package EasyNetQ
  3. Create TextMessage.cs in Messages project:

    namespace Messages
    {
        public class TextMessage
        {
            public string Text { get; set; } 
        }
    }
  4. Add reference to Messages project in both Publisher and Subscriber projects.

    Your solution should look like this:

    Solution explorer

Implementing Publisher and Subscriber Programs

In V8

  1. Publisher Program:

    using EasyNetQ;
    using Messages;
    using Microsoft.Extensions.DependencyInjection;
    
    var serviceCollection = new ServiceCollection();
    serviceCollection.AddEasyNetQ("host=localhost").UseSystemTextJson();
    
    using var provider = serviceCollection.BuildServiceProvider();
    var bus = provider.GetRequiredService<IBus>();
    
    var input = string.Empty;
    Console.WriteLine("Enter a message. 'Quit' to quit.");
    while ((input = Console.ReadLine()) != "Quit")
    {
        await bus.PubSub.PublishAsync(new TextMessage { Text = input });
        Console.WriteLine("Message published!");
    }
  2. Subscriber Program:

    using EasyNetQ;
    using Messages;
    using Microsoft.Extensions.DependencyInjection;
    
    var serviceCollection = new ServiceCollection();
    serviceCollection.AddEasyNetQ("host=localhost").UseSystemTextJson();
    
    using var provider = serviceCollection.BuildServiceProvider();
    var bus = provider.GetRequiredService<IBus>();
    
    await bus.PubSub.SubscribeAsync<TextMessage>("test", HandleTextMessage);
    Console.WriteLine("Listening for messages. Hit <return> to quit.");
    Console.ReadLine();
    
    static void HandleTextMessage(TextMessage textMessage)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Got message: {0}", textMessage.Text);
        Console.ResetColor();
    }

In V7

  1. Publisher Program:

    using EasyNetQ;
    using Messages;
    
    using (var bus = RabbitHutch.CreateBus("host=localhost"))
    {
        var input = string.Empty;
        Console.WriteLine("Enter a message. 'Quit' to quit.");
        while ((input = Console.ReadLine()) != "Quit")
        {
            await bus.PubSub.PublishAsync(new TextMessage { Text = input });
            Console.WriteLine("Message published!");
        }
    }
  2. Subscriber Program:

    using EasyNetQ;
    using Messages;
    
    using (var bus = RabbitHutch.CreateBus("host=localhost"))
    {
        await bus.PubSub.SubscribeAsync<TextMessage>("test", HandleTextMessage);
        Console.WriteLine("Listening for messages. Hit <return> to quit.");
        Console.ReadLine();
    }
    
    static void HandleTextMessage(TextMessage textMessage)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Got message: {0}", textMessage.Text);
        Console.ResetColor();
    }

Running the Applications

  1. Run Subscriber Console Application:

    • Ensure the Subscriber project is set to start first.
    • In your IDE or code editor, run the Subscriber project.
  2. Run Publisher Console Application:

    • Switch to the Publisher project.
    • Run the Publisher project.

    You should now have two console applications running with debugging information showing EasyNetQ has connected to RabbitMQ.

    • Type messages into the publisher console application.
    • The subscriber application should report receiving them.

    Publisher

    Subscriber

Clone this wiki locally