Open Closed

tenantRepository.GetListAsync return error: object reference not set to an instance of an object #4061


User avatar
0
zhongfang created
  • ABP Framework version: v6.0.1
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace: System.NullReferenceException:“Object reference not set to an instance of an object.”
  • Steps to reproduce the issue:" 1)method in background worker 2)ITenantRepository tenantRepository = this.LazyServiceProvider.LazyGetRequiredService<ITenantRepository>(); 3)List<Tenant> allTenants = await tenantRepository.GetListAsync();
Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

10 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share the code of background worker?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    zhongfang created
    public class FansJoinedNotifierWorkItem : AsyncPeriodicBackgroundWorkerBase
    {
        public FansJoinedNotifierWorkItem(AbpAsyncTimer timer, IServiceScopeFactory serviceScopeFactory) : base(timer, serviceScopeFactory)
        {
            this.Timer.Period = 60 * 1000;
        }
    
        protected async override Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
        {
            this.Timer.Period = 1440 * 60 * 1000;
    
            扫码关注送积分 subscribPoint = new 扫码关注送积分();
            subscribPoint.LazyServiceProvider = this.LazyServiceProvider;
            await this.FansSubscribedPoint();
            
            this.Timer.Period = siteOptions.Value.UserFollowNotifyIntervalMinutes * 60 * 1000;
        }
    
        static List<Tenant> allTenants;
        public async Task FansSubscribedPoint()
        {
            Log.Information("Starting: 【扫码关注】积分发放...");
            
            IDataFilter dataFilter = this.LazyServiceProvider.LazyGetRequiredService<IDataFilter>();
    
            if (allTenants == null)
            {
                using (dataFilter.Disable<IMultiTenant>())
                {
                    ITenantRepository tenantRepository = this.LazyServiceProvider.LazyGetRequiredService<ITenantRepository>();
    
                    allTenants = await tenantRepository.GetListAsync();
                }
            }
    
            ……
    
            Log.Information("Completed: 【扫码关注】积分发放...");
        }
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    zhongfang created

    and I try another way,

                ICurrentTenant currentTenant = this.LazyServiceProvider.LazyGetRequiredService&lt;ICurrentTenant&gt;();
                ITenantRepository tenantRepository = this.LazyServiceProvider.LazyGetRequiredService&lt;ITenantRepository&gt;();
    
                if (allTenants == null)
                {
                    using (currentTenant.Change(null))
                    {
                        allTenants = await tenantRepository.GetListAsync();
                    }
                }
    

    got the same error again: Object reference not set to an instance of an object.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Object reference not set to an instance of an object.

    Full error stack?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    zhongfang created

    2022-11-17 11:10:23.791 +08:00 [INF] Starting: 【扫码关注】积分发放... 2022-11-17 11:10:46.619 +08:00 [ERR] Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object. at Yee.Change.AdminBlazorUI.WorkItems.FansJoinedNotifierWorkItem.FansSubscribedPoint() in D:\dev\abp\change\admin-blazor-ui\src\Yee.Change.AdminBlazorUI.Application\WorkItems\FansJoinedNotifierWorkItem.cs:line 97 at Yee.Change.AdminBlazorUI.WorkItems.FansJoinedNotifierWorkItem.DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) in D:\dev\abp\change\admin-blazor-ui\src\Yee.Change.AdminBlazorUI.Application\WorkItems\FansJoinedNotifierWorkItem.cs:line 56 at Volo.Abp.BackgroundWorkers.AsyncPeriodicBackgroundWorkerBase.DoWorkAsync(CancellationToken cancellationToken) 2022-11-17 11:10:51.016 +08:00 [INF] Initialized all ABP modules.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Please set a breaking point to check which object is null/

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    zhongfang created

    Oh...

    ITenantRepository tenantRepository = this.LazyServiceProvider.LazyGetRequiredService<ITenantRepository>();

    tenantRepository is null

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can you reproduce the problem in a new template project? liming.ma@volosoft.com

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    zhongfang created

    No restrict to get the service of ITenantRepository?

    Can I get the ITenantRepository anywhere especially in backgroun worker class?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    We need to check the project.

    https://support.abp.io/QA/Questions/4061#answer-1ab3be6e-393d-7ab9-13c2-3a0796233132

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on October 21, 2026, 11:09
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.