gpt4 book ai didi

c# - Rebus with Simple Injector 重大变更

转载 作者:太空宇宙 更新时间:2023-11-03 19:45:46 37 4
gpt4 key购买 nike

场景:

  • 使用多个卫星库的 Web API 应用程序
  • .NET Framework 4.6
  • 简单注入(inject)器
  • 字谜
  • Rebus.AzureServiceBus
  • Rebus.SimpleInjector

在我的应用程序中有多个附属库,其中大部分都有一个实现 SimpleInjector IPackage 接口(interface)的类,即将不同库中的容器注册进行分组。这些包在启动时注册

container.RegisterPackages(AppDomain.CurrentDomain.GetAssemblies());

其中一个包包含 Rebus 配置

IContainerAdapter adapter = new SimpleInjectorContainerAdapter( container );
Configure.With( adapter )
.Transport( t => t.UseAzureServiceBusAsOneWayClient( connectionString, AzureServiceBusMode.Standard ) )
.Routing( r =>
r.TypeBased()
.MapAssemblyOf<TransactionCreated>( "MyQueue" )
)
.Options( oc => {
oc.SetNumberOfWorkers( 1 );
} )
.Start();

今天早上我们已经将 Rebus 包升级到以下版本:

  • 字谜 4.0.1
  • Rebus.AzureServiceBus 4.0.0
  • Rebus.SimpleInjector 4.0.0

升级后系统停止工作,现在我们得到以下错误

The container can't be changed after the first call to GetInstance, GetAllInstances and Verify. Please see https://simpleinjector.org/locked to understand why the container is locked. The following stack trace describes the location where the container was locked:

通过调试代码,我们可以看到在注册 rebus 的包之后又注册了另一个包,所以错误的原因。我们可以确认没有对代码进行任何修改,并且它在以前的版本中按预期正常工作。

我还可以确认通过降级到这些版本问题就消失了

  • 字谜 3.1.5
  • Rebus.AzureServiceBus 3.0.0
  • Rebus.SimpleInjector 3.0.0

有什么建议吗?

编辑:根据@Steven 的要求,我添加了完整的堆栈跟踪

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The container can't be changed after the first call to GetInstance, GetAllInstances and Verify. Please see https://simpleinjector.org/locked to understand why the container is locked. The following stack trace describes the location where the container was locked:

at Rebus.SimpleInjector.SimpleInjectorContainerAdapter.SetBus(IBus bus)   
at Rebus.Config.RebusConfigurer.Start()
at XXX.YYY.EndpointEvents.Producer.IOC.EndpointEventsProducerModule.RegisterServices(Container container)
at SimpleInjector.PackageExtensions.RegisterPackages(Container container, IEnumerable assemblies)
at XXX.YYY.WebAPI.SimpleInjectorWebApiInitializer.InitializeContainer(Container container)
at XXX.YYY.WebAPI.SimpleInjectorWebApiInitializer.Initialize()
at XXX.YYY.WebAPI.Startup.Configuration(IAppBuilder app)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Owin.Loader.DefaultLoader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder)
at Owin.Loader.DefaultLoader.<>c__DisplayClass1<LoadImplementation>b__0(IAppBuilder builder)
at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.<>c__DisplayClass2.<InitializeBlueprint>b__0(IAppBuilder builder)
at Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action startup)
at Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action startup)
at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint()
at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func valueFactory)
at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context)
at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

最佳答案

很抱歉来晚了对这个问题的 react :)

我终于有时间了解 SimpleInjector 如何进行容器注册,并了解 Rebus 配置 API 如何以某种方式转变为以这种方式工作。

原来是平时的

Configure.With(new MyFavoriteContainerAdapter(container))
.(...)
.Start();

咒语必须以某种方式移动到 Func<IBus> 中,这样就可以在实际启动总线之前完成所有与 Rebus 相关的注册(以及您自己的注册)。

结果(刚刚在 NuGet.org 上的 Rebus.SimpleInjector 5.0.0-b01 中出现)是这个 API:

public class RebusPackage : IPackage
{
public void RegisterServices(Container container)
{
Console.WriteLine("Calling RebusPackage");

container.ConfigureRebus(
configurer => configurer
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "test"))
.Start()
);

}
}

如果您正在使用 SimpleInjector.Packaging 或简单地使用它,它会是这样的

container.ConfigureRebus(
configurer => configurer
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "test"))
.Start()
);

如果你只有一个 SimpleInjector container .

当你认为是时候启动公共(public)汽车时,你要么

container.StartBus();

或者你等到IBus已解决。

关于c# - Rebus with Simple Injector 重大变更,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45886811/

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