簡體   English   中英

EasyNetQ 訂閱不適用於 TopShelf

[英]EasyNetQ Subscribe not working with TopShelf

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

我不知道我哪里錯了!

這里:

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

該代碼在訂閱后立即處理與 EasyNetQ 的連接 - 這將斷開連接並再次終止訂閱。 根據EasyNetQ 文檔

標准做法是為應用程序的生命周期創建單個 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM