Handle Publish/Subscribe in Rebus with Azure Service Bus

Viewed 660

I am looking into Rebus and to use it with Azure Service Bus. Using it with regalure Queues was easy, but when I want to use Topic instead I can't get it to work.

Is there any here that have a done a setup and use it with Topic/Subscription. This is what I have so far.

        static void Main(string[] args)
    {
        _bus1 = InitializeBus(System.Environment.MachineName);
        _bus2 = InitializeBus(System.Environment.MachineName + "_2");
        _bus3 = InitializeBus();

        Run();
        Console.WriteLine("Press Enter to exit!");
        Console.ReadLine();
    }

    private static void Run()
    {
        try
        {
            _bus1.Handle<string>((b, c, m) => { Console.WriteLine(m); return null; });
            _bus2.Handle<string>((b, c, m) => { Console.WriteLine(m); return null; });
            _bus1.Bus.Subscribe<string>();
            _bus2.Bus.Subscribe<string>();
            _bus3.Bus.Publish("Publish test message");
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    private static BuiltinHandlerActivator InitializeBus(string queueName = null)
    {
        var activator = new BuiltinHandlerActivator();

        if(string.IsNullOrEmpty(queueName))
            Configure.With(activator)
                .Transport(t => t.UseAzureServiceBusAsOneWayClient(connectionString))
                .Options(o => { o.SetNumberOfWorkers(10); o.SetMaxParallelism(10); })
                .Start();
        else
            Configure.With(activator)
                .Transport(t => t.UseAzureServiceBus(connectionString, queueName).EnablePartitioning().DoNotCreateQueues())
                .Options(o => { o.SetNumberOfWorkers(10); o.SetMaxParallelism(10); })
                .Start();

        return activator;
    }

First I create all the buses. I am using DontCreateQueues() since I don't want the queues to be duplicated created in my root but only under the Topic as Subscription. Then I set up the buses and the Publish works fine, there is one Topic created and 2 subscriptions created under this Topic, and there is 1 message in each of this subscriptions. But the messages is never collected.

If I remove the DontCreateQueues() method in the Configuration the code work, but then 2 queues are created in the root togheter with the topic and it's 2 subscriptions, but I can't have it like that.

Best Regards Magnus

1 Answers
Related