gpt4 book ai didi

asp.net-mvc - 根据内容重排多个队列

转载 作者:行者123 更新时间:2023-12-04 03:13:50 24 4
gpt4 key购买 nike

设置:在 asp.net mvc 项目中使用 SimpleInjector 进行 Rebus。

我需要创建两个接收消息的处理程序,每个处理程序都来自一个特定的队列。按照我在此 SO answer 上找到的内容进行操作我创建了类似的代码。

在一个类库中,我有一个实现 SimpleInjector IPackage 的类,它的代码如下:

public void RegisterServices( Container container ) {
container.Register<IHandleMessages<MyMessage>, MyMessageHandler>( Lifestyle.Scoped );
IContainerAdapter adapter = new SimpleInjectorContainerAdapter( container );
Configure.With( adapter )
.Transport( t => t.UseAzureServiceBus( connectionString, "A_QUEUE_NAME", AzureServiceBusMode.Standard ) )
.Options( oc => {
oc.SetNumberOfWorkers( 1 );
oc.SimpleRetryStrategy( errorQueueAddress: "A_ERROR_QUEUE_NAME", maxDeliveryAttempts: 3 );
} )
.Start();

Configure.With(adapter
.Transport(t => t.UseAzureServiceBus(connectionString, "B_QUEUE_NAME")
.Options( oc => {
oc.SetNumberOfWorkers( 1 );
oc.SimpleRetryStrategy( errorQueueAddress: "B_ERROR_QUEUE_NAME", maxDeliveryAttempts: 3 );
} )
.Start();
}

但是,当调试器进入第二个 Configure.With( ... ) 调用时,i 会以错误提示终止:

Type IBus has already been registered. If your intention is to resolve a collection of IBus implementations, use the RegisterCollection overloads. More info: https://simpleinjector.org/coll1. If your intention is to replace the existing registration with this new registration, you can allow overriding the current registration by setting Container.Options.AllowOverridingRegistrations to true. More info: https://simpleinjector.org/ovrrd.

堆栈跟踪:

[InvalidOperationException: Type IBus has already been registered. If your intention is to resolve a collection of IBus implementations, use the RegisterCollection overloads. More info: https://simpleinjector.org/coll1. If your intention is to replace the existing registration with this new registration, you can allow overriding the current registration by setting Container.Options.AllowOverridingRegistrations to true. More info: https://simpleinjector.org/ovrrd.]
SimpleInjector.Internals.NonGenericRegistrationEntry.ThrowWhenTypeAlreadyRegistered(InstanceProducer producer) +102
SimpleInjector.Internals.NonGenericRegistrationEntry.Add(InstanceProducer producer) +59
SimpleInjector.Container.AddInstanceProducer(InstanceProducer producer) +105
SimpleInjector.Container.AddRegistrationInternal(Type serviceType, Registration registration) +69
SimpleInjector.Container.AddRegistration(Type serviceType, Registration registration) +131
SimpleInjector.Container.RegisterSingleton(TService instance) +183
Rebus.SimpleInjector.SimpleInjectorContainerAdapter.SetBus(IBus bus) +55
Rebus.Config.RebusConfigurer.Start() +2356
MyModule.RegisterServices(Container container) +497
SimpleInjector.PackageExtensions.RegisterPackages(Container container, IEnumerable`1 assemblies) +50
Myproject.SimpleInjectorInitializer.InitializeContainer(Container container) +35
Myproject.SimpleInjectorInitializer.Initialize() +68
Myproject.Startup.Configuration(IAppBuilder app) +28

编辑

然后我删除了第二个 Configure.With( ... ) 代码块,现在当我执行 _bus.Send(message) 时,我得到另一个错误在消费者过程中说

Unhandled exception 1 while handling message with ID fef3acca-97f4-4495-b09d-96e6c9f66c4d: SimpleInjector.ActivationException: No registration for type IEnumerable<IHandleMessages<MyMessage>> could be found. There is, however, a registration for IHandleMessages<MyMessage>; Did you mean to call GetInstance<IHandleMessages<MyMessage>>() or depend on IHandleMessages<MyMessage>? Or did you mean to register a collection of types using RegisterCollection?

堆栈跟踪:

2017-04-13 10:21:03,805 [77] WARN  Rebus.Retry.ErrorTracking.InMemErrorTracker - 
at SimpleInjector.Container.ThrowMissingInstanceProducerException(Type serviceType)
at SimpleInjector.Container.GetInstanceForRootType[TService]()
at SimpleInjector.Container.GetInstance[TService]()
at SimpleInjector.Container.GetAllInstances[TService]()
at Rebus.SimpleInjector.SimpleInjectorContainerAdapter.<GetHandlers>d__3`1.MoveNext()

最佳答案

我通常建议每个容器实例只保留一个 IBus,因为总线本身可以被视为“应用程序”,这恰好与 IoC 容器是一个对象这一事实非常吻合。可以在其生命周期内“托管”应用程序。

Rebus 不提供 Conforming Container 抽象,因为我同意 Mark Seemann 的观点,即这是一个注定要失败的项目。事实上,如 the wiki page mentions , Rebus 曾经提供处理程序的自动注册,但结果证明是有问题的。

相反,Rebus 鼓励您提供一个“容器适配器”(IContainerAdapter 的实现),其职责是:

  • 查找处理程序
  • 提供一种以正确方式注册IBusIMessageContext的方法

为 Autofac、CaSTLe Windsor、SimpleInjector 等提供开箱即用的容器适配器。但是,不需要提供容器适配器 - Configure.With(...) 咆哮是很高兴只收到一个“处理程序激活器”(IHandlerActivator 的实现),所以如果您只想使用您的 IoC 容器来查找处理程序并自己注册 IBus ,您也可以通过实现 IHandlerActivator 并在容器中查找处理程序来做到这一点。

TL;DR:Rebus 方式是将 IoC 容器的一个实例视为一个单独的应用程序,因此只在其中注册一个 IBus 是有意义的.

在单个进程中新建多个容器实例是非常好的,您希望托管多个应用程序(甚至是具有不同消息 SLA 的应用程序的多个实例)。

关于asp.net-mvc - 根据内容重排多个队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43385332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com