使用Azure Service Bus具有集中订阅存储的发布子模式

时间:2019-04-24 15:04:54

标签: rebus rebus-azureservicebus

我是Rebus的新手,并尝试使用Azure Service Bus运行以下应用程序,但出现以下错误: 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

我的问题是,由于不支持订阅,我该如何使其工作?

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

1 个答案:

答案 0 :(得分:0)

使用Azure Service Bus,无需注册订阅存储,因为它本身支持发布/订阅。

这意味着您可以安全地注释掉

.Subscriptions(s => ...)

事物,然后一切都会正常工作。