-
Notifications
You must be signed in to change notification settings - Fork 754
Quick Start
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.
-
Run RabbitMQ Docker Container:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
-
Navigate to RabbitMQ Management URL:
http://localhost:15672/
-
Create a new solution named
EasyNetQTest
with three C# projects:-
Messages
(Class Library) -
Publisher
(Console Application) -
Subscriber
(Console Application)
-
-
Install EasyNetQ:
dotnet add Publisher package EasyNetQ dotnet add Subscriber package EasyNetQ
-
Create
TextMessage.cs
inMessages
project:namespace Messages { public class TextMessage { public string Text { get; set; } } }
-
Add reference to
Messages
project in bothPublisher
andSubscriber
projects.Your solution should look like this:
-
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!"); }
-
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(); }
-
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!"); } }
-
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(); }
-
Run Subscriber Console Application:
- Ensure the
Subscriber
project is set to start first. - In your IDE or code editor, run the
Subscriber
project.
- Ensure the
-
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.
- Switch to the
- Quick Start
- Introduction
- A Note on Versioning
- Installing EasyNetQ
- Connecting to RabbitMQ
- Connecting with SSL
- Logging
- Logging v8.x
- Publish
- Subscribe
- Request Response
- Send Receive
- Topic Based Routing
- Controlling Queue names
- Polymorphic Publish and Subscribe
- Versioning Messages
- Publisher Confirms
- Scheduling Events with Future Publish
- Support for Delayed Messages Plugin
- Auto Subscriber
- Auto Subscriber v8.x
- Error Conditions
- Re Submitting Error Messages With EasyNetQ.Hosepipe
- The Advanced API
- Cluster Support
- Wiring up EasyNetQ with TopShelf and Windsor
- Replacing EasyNetQ Components
- Using Alternative DI Containers
- Enable Legacy Conventions