rebusrebus-azureservicebus

Pub sub pattern with centralized subscription storage using Azure Service Bus


I am new to Rebus, and try to run the app below with Azure Service Bus, but I got the error below: https://github.com/rebus-org/RebusSamples/tree/master/PubSubCentralized

System.InvalidOperationException
  HResult=0x80131509
  Message=Attempted to register primary -> Rebus.Subscriptions.ISubscriptionStorage, but a primary registration already exists: primary -> Rebus.Subscriptions.ISubscriptionStorage (The Azure Service Bus transport was inserted as the subscriptions storage because it has native support for pub/sub messaging)
  Source=Rebus
  StackTrace:
   at Rebus.Injection.Injectionist.Register[TService](Func`2 resolverMethod, Boolean isDecorator, String description)
   at Rebus.Injection.Injectionist.Register[TService](Func`2 resolverMethod, String description)
   at Publisher.Program.<>c.<Main>b__2_2(StandardConfigurer`1 s) in C:\ReBus\PubSub\Publisher\Program.cs:line 26
   at Rebus.Config.RebusConfigurer.Subscriptions(Action`1 configurer)
   at Publisher.Program.Main() in C:\ReBus\PubSub\Publisher\Program.cs:line 22

My question is that How can I make it work because Subscriptions is not supported?

 class Subscriber
    {
        static void Main()
        {
            using (var activator = new BuiltinHandlerActivator())
            {
                activator.Register(() => new Handler());

                Configure.With(activator)
                    .Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))                                 
                    .Transport(t => t.UseAzureServiceBus(Consts.ServiceBusConnectionString, Consts.Subscriber))
                   // .Subscriptions(s => s.StoreInSqlServer("server=.; database=RebusPubSubCentralized; trusted_connection=true", "Subscriptions", isCentralized: true))
                    .Start();

                activator.Bus.Subscribe<StringMessage>().Wait();
                activator.Bus.Subscribe<DateTimeMessage>().Wait();
                activator.Bus.Subscribe<TimeSpanMessage>().Wait();

                Console.WriteLine("This is Subscriber 1");
                Console.WriteLine("Press ENTER to quit");
                Console.ReadLine();
                Console.WriteLine("Quitting...");
            }
        }
    }




class Publisher
    {
        static void Main()
        {
            using (var activator = new BuiltinHandlerActivator())
            {
                Configure.With(activator)
                    .Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))
                    .Transport(t => t.UseAzureServiceBus(Consts.ServiceBusConnectionString, Consts.Publisher))                 
                    //.Subscriptions(s => s.StoreInSqlServer("server=.; database=RebusPubSubCentralized; trusted_connection=true", "Subscriptions", isCentralized: true))
                    .Start();

                var startupTime = DateTime.Now;

                var keyChar = char.ToLower(Console.ReadKey(true).KeyChar);
                var bus = activator.Bus.Advanced.SyncBus;
                bus.Publish(new TimeSpanMessage(DateTime.Now - startupTime));                    

                Console.WriteLine("Quitting!");
            }
        }
    }   

Solution

  • With Azure Service Bus there's no need for registering a subscription storage, because it natively supports publish/subscribe.

    This means that you can safely comment out the

    .Subscriptions(s => ...)
    

    thing, and then everything will work.