我正在尝试将Rebus与Azure Service Bus和Castle Windsor一起使用。当我使用没有Castle Windsor的代码版本时,队列和主题都会正确创建:
public static void Main(string[] args)
{
using (var activator = new BuiltinHandlerActivator())
{
activator.Register(() => new SomeMessageHandler());
activator.Register(() => new BusItemMessageHandler());
Configure.With(activator)
.Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))
.Transport(t => t.UseAzureServiceBus(GetConnectionString(), "ReceiverInputQueue"))
.Start();
activator.Bus.Subscribe<SomeMessage>().Wait();
activator.Bus.Subscribe<IBusItem>().Wait();
Console.ReadLine();
Console.WriteLine("Quitting...");
}
}
但当我尝试使用Castle Windsor时,只创建了队列(没有主题(:
public static void Main(string[] args)
{
using (var container = new WindsorContainer())
{
//container.Register(Classes.FromAssemblyContaining<BusItemMessageHandler>()
// .BasedOn<IHandleMessages>()
// .WithServiceAllInterfaces()
// .LifestyleTransient());
container.AutoRegisterHandlersFromAssemblyOf<BusItemMessageHandler>();
var adapter = new CastleWindsorContainerAdapter(container);
Configure.With(adapter)
.Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))
.Transport(t => t.UseAzureServiceBus(GetConnectionString(), "ReceiverInputQueue"))
.Start();
Console.WriteLine("Press ENTER to quit");
Console.ReadLine();
Console.WriteLine("Quitting...");
}
}
这里的消息/处理程序:
public class BusItemMessageHandler : IHandleMessages<IBusItem>
{
public async Task Handle(IBusItem message)
{
var a = message;
}
}
public class SomeMessageHandler : IHandleMessages<SomeMessage>
{
public async Task Handle(SomeMessage message)
{
Console.WriteLine($"Got string: {message.Message}");
}
}
public class SomeMessage : IBusItem
{
public Guid BusItemId { get; set; } = Guid.NewGuid();
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
public string Message { get; set; }
}
怎么了?有什么建议吗?谢谢
使用Rebus和Azure服务总线,在时创建主题
a( 出版商首次向其发布,或b( 用户首先订阅
当我比较你的两个代码片段时,这两行与
activator.Bus.Subscribe<SomeMessage>().Wait();
activator.Bus.Subscribe<IBusItem>().Wait();
代码段2中缺少。
如果你这样做:
var bus = container.Resolve<ISyncBus>(); //< NOTE: SYNC BUS!
bus.Subscribe<SomeMessage>();
我打赌你会得到预期的题目!🙂
PS:除非修改Rebus将.NET类型映射到主题的方式,否则订阅IBusItem
无效。当您发布SomeMessage
的实例时,它将被发布到以该类型命名的主题(例如SomeNamespace.SomeMessage__SomeAssembly
(。
不过,您仍然可以让一个处理程序实现IHandleMessages<IBusItem>
。多态调度机制在收到消息后启动,因此只要您订阅了所有必要的类型,调度机制就会调度到所有兼容的处理程序。