奥尔良ServiceProvider替换温莎城堡引发循环依赖异常

时间:2018-08-01 07:03:39

标签: castle-windsor orleans

奥尔良ServiceProvider取代温莎城堡引发循环依赖异常。

  
    

Castle.MicroKernel.CircularDependencyException:“尝试解析组件时已检测到依赖周期     'Orleans.Runtime.ActivationData + GrainActivationContextFactory_a4cef614-bbd0-4a86-a9e2-97c9b018687b'。     导致循环的分辨率树如下:     零件     'Orleans.Runtime.ActivationData + GrainActivationContextFactory_a4cef614-bbd0-4a86-a9e2-97c9b018687b'     解析为组件的依赖关系     'Orleans.Runtime.IGrainActivationContext_6e469552-2d8e-4f0d-9264-d873b807dfe2'     解析为组件的依赖关系     'Orleans.Runtime.ActivationData + GrainActivationContextFactory_a4cef614-bbd0-4a86-a9e2-97c9b018687b'     这是要解析的根组件。 ”

  

我使用WindsorRegistrationHelper CreateServiceProvider方法。

var builder = new SiloHostBuilder();
builder.UseServiceProviderFactory(services =>
{
    var abpBootstrapper = AbpBootstrapper.Create<LimsServerModule>();
    abpBootstrapper.Initialize();

    return WindsorRegistrationHelper.CreateServiceProvider(abpBootstrapper.IocManager.IocContainer, services);
});

我认为问题在于这是循环依赖 奥尔良托管DefaultSiloServices代码片段: DefaultSiloServices.cs

services.TryAddScoped<ActivationData.GrainActivationContextFactory>();
services.TryAddScoped<IGrainActivationContext>(sp => sp.GetRequiredService<ActivationData.GrainActivationContextFactory>().Context);

这是WindsorRegistrationHelper RegisterServiceDescriptor代码段,

private static void RegisterServiceDescriptor(IWindsorContainer container, ServiceDescriptor serviceDescriptor)
{
    string uniqueName = serviceDescriptor.ServiceType.FullName + "_" + Guid.NewGuid();

    if (serviceDescriptor.ImplementationType != null)
    {
        container.Register(
            Component.For(serviceDescriptor.ServiceType)
                     .Named(uniqueName)
                     .IsDefault()
                     .ImplementedBy(serviceDescriptor.ImplementationType)
                     .ConfigureLifecycle(serviceDescriptor.Lifetime));
    }
    else if (serviceDescriptor.ImplementationFactory != null)
    {
        var serviceDescriptorRef = serviceDescriptor;
        container.Register(
            Component.For(serviceDescriptor.ServiceType)
                     .Named(uniqueName)
                     .IsDefault()
                     .UsingFactoryMethod(c => serviceDescriptorRef.ImplementationFactory(c.Resolve<IServiceProvider>()))
                     .ConfigureLifecycle(serviceDescriptor.Lifetime)
        );
    }
    else
    {
        container.Register(
            Component.For(serviceDescriptor.ServiceType)
                     .Named(uniqueName)
                     .IsDefault()
                     .Instance(serviceDescriptor.ImplementationInstance)
                     .ConfigureLifecycle(serviceDescriptor.Lifetime)
        );
    }
}

该如何解决循环依赖问题?请帮助我

0 个答案:

没有答案