2

I am new to this combination of RabbitMQ, EasyNetQ, TopShelf. At the moment, I am not using any DI.

I am trying to subscribe queue using EasyNetQ. Subscribe works with this console application code

class Program
{
    static void Main(string[] args)
    {
        using (var bus = RabbitHutch.CreateBus("host=localhost"))
        {
            bus.Subscribe<Entity>("entity", Handler);
            Console.ReadLine();

        }
    }

    private static void Handler(Entity obj)
    {
        Console.WriteLine($"{obj.ID}, {obj.Name}");
    }
}

With TopShelf, it doesn't hit Handler method. I don't see any error reported by TopShelf or EasyNetQ

class Program
{
    static void Main(string[] args)
    {
        HostFactory.Run(config =>
        {
            config.Service<TestEasyNet>(service =>
            {
                service.ConstructUsing(s => new TestEasyNet());
                service.WhenStarted(s => s.Start());
                service.WhenStopped(s => s.Stop());
            });

            config.SetServiceName("TestSubscribe");
            config.SetDisplayName("Test Subscribe");
            config.SetDescription("Test Subscribe");
        });
    }
}

class TestEasyNet
{
    public void Start()
    {
        using (var bus = EasyNetQ.RabbitHutch.CreateBus("host=localhost"))
        {
            bus.Subscribe<Entity>("entity", Handler);
        }
    }

    private void Handler(Entity obj)
    {
        Console.WriteLine("Subscribing");
        Console.WriteLine($"{obj.ID}, {obj.Name}");
    }

    public void Stop()
    { }
}

Message publishing code is

class Program
{
    static void Main(string[] args)
    {
        HostFactory.Run(c =>
        {
            c.Service<Hosting>(service =>
            {
                service.ConstructUsing(s => new Hosting());
                service.WhenStarted(s => s.Start());
                service.WhenStopped(s => s.Stop());
            });
            c.SetServiceName("TempService");
            c.SetDisplayName("Temp Service");
            c.SetDescription("Temp Service");
        });
    }
}

public class Hosting
{
    public void Start()
    {
        var entity = new Entity()
        {
            ID = 1,
            Name = "Entity 1"
        };

        var entity2 = new Entity()
        {
            ID = 2,
            Name = "Entity 2"
        };

        var entity3 = new Entity()
        {
            ID = 3,
            Name = "Entity 3"
        };

        using (var bus = RabbitHutch.CreateBus("host=localhost"))
        {
            bus.Publish<Entity>(entity);
            bus.Publish<Entity>(entity2);
            bus.Publish<Entity>(entity3);
        }
    }

    public void Stop()
    {             
    }
}

I couldn't get where am I going wrong!

crazy coding
  • 281
  • 2
  • 10

1 Answers1

3

Here:

using (var bus = EasyNetQ.RabbitHutch.CreateBus("host=localhost"))
{
    bus.Subscribe<Entity>("entity", Handler);
}

the code disposes of the connection to EasyNetQ immediately after subscribing - which will disconnect and terminate the subscription again. Per the EasyNetQ documentation:

Standard practice is to create a single IBus instance for the lifetime of your application. Dispose it when your application closes.

In this case, you probably want to tie the lifecycle of the EasyNetQ bus to the service being started or stopped via TopShelf. So:

private IBus bus;

public void Start()
{
    bus = EasyNetQ.RabbitHutch.CreateBus("host=localhost"));
    bus.Subscribe<Entity>("entity", Handler);
}

public void Stop()
{
    bus?.Dispose();
    bus = null;
}
mountain traveller
  • 7,591
  • 33
  • 38