Open Closed

DependencyResolutionException: Circular component dependency detected: [ExposeServices(typeof(ICurrentTenant), typeof(CurrentTenant), typeof(MyCurrentTenant))] #6062


User avatar
0
joerguth created

Based on https://support.abp.io/QA/Questions/2359/CurrentTenant--Extra-Property

I tried to implement this in a new Module and using the Web.Unified Project to check it, but the constructor with ITenantRepository throws an exception at Autofac. What can I do ?

<br> using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; using Volo.Abp.MultiTenancy; using Volo.Saas.Tenants; namespace ReplaceICurrentTenant.AbpExtented; [Dependency(ReplaceServices = true)] [ExposeServices(typeof(ICurrentTenant), typeof(CurrentTenant), typeof(MyCurrentTenant))] public class MyCurrentTenant : CurrentTenant { private readonly ITenantRepository _tenantRepository; public MyCurrentTenant(ICurrentTenantAccessor currentTenantAccessor, ITenantRepository tenantRepository ) : base(currentTenantAccessor) { } public async Task<Dictionary<string, object>> GetExtraProperties() { if (!base.Id.HasValue) { return new Dictionary<string, object>(); } //var tenant = await _tenantRepository.GetAsync((Guid)base.Id); //implement your logic //tenant.ExtraProperty return new Dictionary<string, object>(); } }

Here the exception: Autofac.Core.DependencyResolutionException HResult=0x80131500 Nachricht = An exception was thrown while activating Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionEndpointDataSourceFactory -> Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider -> λ:Microsoft.AspNetCore.Mvc.Abstractions.IActionDescriptorProvider[] -> Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider -> Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory -> λ:Microsoft.AspNetCore.Mvc.ApplicationModels.IApplicationModelProvider[] -> Microsoft.AspNetCore.Mvc.ApplicationModels.AuthorizationApplicationModelProvider -> Volo.Abp.Authorization.AbpAuthorizationPolicyProvider -> Volo.Abp.Authorization.Permissions.PermissionDefinitionManager -> Volo.Abp.PermissionManagement.DynamicPermissionDefinitionStore -> Volo.Abp.PermissionManagement.EntityFrameworkCore.EfCorePermissionGroupDefinitionRecordRepository -> Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1[[Volo.Abp.PermissionManagement.EntityFrameworkCore.IPermissionManagementDbContext, Volo.Abp.PermissionManagement.EntityFrameworkCore, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]] -> Volo.Abp.MultiTenancy.MultiTenantConnectionStringResolver -> ReplaceICurrentTenant.AbpExtented.MyCurrentTenant -> Volo.Saas.EntityFrameworkCore.EfCoreTenantRepository -> Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1[[Volo.Saas.EntityFrameworkCore.ISaasDbContext, Volo.Saas.EntityFrameworkCore, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]]. Quelle = Autofac Stapelüberwachung: bei Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action`1 next) bei Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) bei Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt) bei Autofac.Core.Resolving.Middleware.RegistrationPipelineInvokeMiddleware.Execute(ResolveRequestContext context, Action`1 next) bei Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.&lt;BuildPipeline>b__1(ResolveRequestContext ctxt) bei Autofac.Core.Resolving.Middleware.SharingMiddleware.<>c__DisplayClass5_0.&lt;Execute>b__0() bei Autofac.Core.Lifetime.LifetimeScope.CreateSharedInstance(Guid id, Func`1 creator) bei Autofac.Core.Lifetime.LifetimeScope.CreateSharedInstance(Guid primaryId, Nullable`1 qualifyingId, Func`1 creator) bei Autofac.Core.Resolving.Middleware.SharingMiddleware.Execute(ResolveRequestContext context, Action`1 next) bei Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.&lt;BuildPipeline>b__1(ResolveRequestContext ctxt) bei Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.&lt;BuildPipeline>b__1(ResolveRequestContext ctxt) bei Autofac.Core.Resolving.Middleware.CircularDependencyDetectorMiddleware.Execute(ResolveRequestContext context, Action`1 next) bei Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.<>c__DisplayClass14_0.<BuildPipeline>b__1(ResolveRequestContext ctxt) bei Autofac.Core.Pipeline.ResolvePipeline.Invoke(ResolveRequestContext ctxt) bei Autofac.Core.Resolving.ResolveOperation.InvokePipeline(ResolveRequest request, DefaultResolveRequestContext requestContext) bei Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request) bei Autofac.Core.Resolving.ResolveOperation.ExecuteOperation(ResolveRequest request) bei Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request) bei Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request) bei Autofac.Core.Container.ResolveComponent(ResolveRequest request) bei Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) bei Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters) bei Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters) bei Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType) bei Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType) bei Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) bei Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints) bei Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllerRoute(IEndpointRouteBuilder endpoints, String name, String pattern, Object defaults, Object constraints, Object dataTokens) bei Volo.Abp.AspNetCore.Mvc.AbpAspNetCoreMvcModule.<>c.&lt;ConfigureServices>b__1_13(EndpointRouteBuilderContext endpointContext) bei Microsoft.AspNetCore.Builder.AbpAspNetCoreApplicationBuilderExtensions.<>c__DisplayClass0_0.&lt;UseConfiguredEndpoints>b__0(IEndpointRouteBuilder endpoints) bei Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure) bei Microsoft.AspNetCore.Builder.AbpAspNetCoreApplicationBuilderExtensions.UseConfiguredEndpoints(IApplicationBuilder app, Action`1 additionalConfigurationAction) bei ReplaceICurrentTenant.ReplaceICurrentTenantWebUnifiedModule.OnApplicationInitialization(ApplicationInitializationContext context) in D:\Temp\ReplaceICurrentTenant\host\ReplaceICurrentTenant.Web.Unified\ReplaceICurrentTenantWebUnifiedModule.cs: Zeile203 bei Volo.Abp.Modularity.AbpModule.OnApplicationInitializationAsync(ApplicationInitializationContext context) bei Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor.&lt;InitializeAsync>d__0.MoveNext() Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst: [Externer Code] Innere Ausnahme 1: DependencyResolutionException: Circular component dependency detected: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionEndpointDataSourceFactory -> Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider -> λ:Microsoft.AspNetCore.Mvc.Abstractions.IActionDescriptorProvider[] -> Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider -> Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory -> λ:Microsoft.AspNetCore.Mvc.ApplicationModels.IApplicationModelProvider[] -> Microsoft.AspNetCore.Mvc.ApplicationModels.AuthorizationApplicationModelProvider -> Volo.Abp.Authorization.AbpAuthorizationPolicyProvider -> Volo.Abp.Authorization.Permissions.PermissionDefinitionManager -> Volo.Abp.PermissionManagement.DynamicPermissionDefinitionStore -> Volo.Abp.PermissionManagement.EntityFrameworkCore.EfCorePermissionGroupDefinitionRecordRepository -> Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1[[Volo.Abp.PermissionManagement.EntityFrameworkCore.IPermissionManagementDbContext, Volo.Abp.PermissionManagement.EntityFrameworkCore, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]] -> Volo.Abp.MultiTenancy.MultiTenantConnectionStringResolver -> ReplaceICurrentTenant.AbpExtented.MyCurrentTenant -> Volo.Saas.EntityFrameworkCore.EfCoreTenantRepository -> Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1[[Volo.Saas.EntityFrameworkCore.ISaasDbContext, Volo.Saas.EntityFrameworkCore, Version=7.4.0.0, Culture=neutral, PublicKeyToken=null]] -> Volo.Abp.MultiTenancy.MultiTenantConnectionStringResolver.


4 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    The ICurrentTenant interface is a low-level API and a lot of services depend on it.

    This is why there are circular dependencies. You should create a new service. for example:

    public interface ICurrentTenantInfo
    {
        Task<Dictionary<string, object>> GetExtraProperties();
    }
    
    public class CurrentTenantInfo : ICurrentTenantInfo, ITransientDependency
    {
        private readonly ICurrentTenant _currentTenant;
        private readonly ITenantRepository _tenantRepository;
    
        public CurrentTenantInfo(ICurrentTenant currentTenant, ITenantRepository tenantRepository)
        {
            _currentTenant = currentTenant;
            _tenantRepository = tenantRepository;
        }
    
        public async Task<Dictionary<string, object>> GetExtraProperties()
        {
            if(!_currentTenant.IsAvailable) 
            {    
                return new Dictionary<string, object>();
            }
           
            var tenant = await _tenantRepository.GetAsync(_currentTenant.GetId());
            //implement your logic
            //tenant.ExtraProperty ...
            return new Dictionary<string, object>();
        }
    }
    
  • User Avatar
    0
    joerguth created

    Thank you,

    just to be sure, then only inject ICurrentTenantInfo in the ctors? to use it.

    Regards

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Yes

  • User Avatar
    0
    joerguth created

    Perfect, thanks

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35