usls-rv
usls-rv

Reputation: 89

How to separate MassTransit demo into producer and consumer applications

I am trying to separate the MassTransit demo (https://masstransit-project.com/MassTransit/quickstart.html) into two separate applications, but my consumer application is not receiving any messages.

I have created 3 projects: Send, Receive, and Messages. Send contains a Send class which is my producer code, Receive contains a Receive class which is my consumer code, and Messages contains classes for my messages.

Here is my Send class:

using MassTransit;
using Messages;
using System;

namespace MassTransitTest
{
    class Send
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(config =>
            {
                var host = config.Host(new Uri("rabbitmq://localhost"), c =>
                {
                    c.Username("guest");
                    c.Password("guest");
                });
            });

            bus.Start();

            Console.WriteLine("Publishing message");

            bus.Publish(new TextMessage() { Text = "Testing 12345" });

            bus.Stop();

            Console.ReadLine();
        }
    }
}

here is my Receive class:

using MassTransit;
using Messages;
using System;

namespace Receive
{
    class Receive
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(config =>
            {
                var host = config.Host(new Uri("rabbitmq://localhost"), c =>
                {
                    c.Username("guest");
                    c.Password("guest");
                });

                config.ReceiveEndpoint(host, "queue", endpoint =>
                {
                    endpoint.Handler<TextMessage>(context =>
                    {
                        return Console.Out.WriteLineAsync($"{context.Message.Text}");
                    });
                });
            });

            bus.Start();

            Console.WriteLine("Receive listening for messages");

            Console.ReadLine();

            bus.Stop();
        }
    }
}

Finally, here is my TextMessage class:

using System;

namespace Messages
{
    public class TextMessage
    {
        public string Text { get; set; }
    }
}

When I send the message from my Send class I would like to receive it and output it to the console from my Receive class.

Upvotes: 2

Views: 1728

Answers (1)

usls-rv
usls-rv

Reputation: 89

I figured it out! Publish is an asynchronous message that returns a Task, so I needed to await and then everything worked. My Send program was exiting before the message was fired off.

Upvotes: 3

Related Questions