【问题标题】:EasyNetQ Subscribe not working with TopShelfEasyNetQ 订阅不适用于 TopShelf
【发布时间】:2019-02-27 16:32:25
【问题描述】:

我是 RabbitMQ、EasyNetQ、TopShelf 组合的新手。 目前,我没有使用任何 DI。

我正在尝试使用 EasyNetQ 订阅队列。 订阅与此控制台应用程序代码一起使用

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}");
    }
}

使用 TopShelf,它不会命中 Handler 方法。我没有看到 TopShelf 或 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()
    { }
}

消息发布代码是

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()
    {             
    }
}

我不知道我哪里错了!

【问题讨论】:

  • 编辑了我的原始答案以链接到通过 TopShelf 管理的服务生命周期

标签: .net rabbitmq topshelf easynetq


【解决方案1】:

这里:

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

代码在订阅后立即处理与 EasyNetQ 的连接 - 这将断开连接并再次终止订阅。根据EasyNetQ documentation

标准做法是在应用程序的整个生命周期内创建一个 IBus 实例。在您的应用程序关闭时处理它。

在这种情况下,您可能希望将 EasyNetQ 总线的生命周期与通过 TopShelf 启动或停止的服务联系起来。所以:

private IBus bus;

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

public void Stop()
{
    bus?.Dispose();
    bus = null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 2021-05-18
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2020-08-11
    相关资源
    最近更新 更多