Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

IRepository<BinaryObject, Guid> not accessing records in Background Job. #9462


User avatar
0
TimMackey created

I created a Background Job per the following instructions: Background Jobs

However, the _binaryObjectRepository does not appear to be connected to the db. var _all = _binaryObjectRepository.GetAllList(); does not cause Exceptions, but also does not return any values. How can I get this to work?

Also, maybe for another issue, _localizationManager only returns English language string - ignores all other language settings - returns English strings. These issues only appear in a Background Job. They work as expected in AppServices. Another clue: _settingManager works correctly as expected.

using Abp.Application.Services;
using Abp.BackgroundJobs;
using Abp.Configuration;
using Abp.Domain.Uow;
using Abp.Localization;
using Abp.Runtime.Session;
using Abp.Dependency;
using Abp.EntityFrameworkCore;
using ExceptionsLibrary;
using JobsLibrary;
using Microsoft.AspNetCore.Hosting;
using ngTTM.Storage;
using System;
using System.IO;
using Abp.Domain.Repositories;

#if EXAM_DB_CONTEXT_LIB
using DbContext = ExamDbContextLibrary.ExamDbContext;
#else
using ngTTM.TtmDataModel;
using DbContext = ngTTM.EntityFrameworkCore.ngTTMDbContext;
#endif

namespace JobManager
{
    //-----------------------------------------------------------------------------------------
    [Serializable]
    public class CreateExamJobArgs
    {
        public long userId { get; set; }
        public string guid { get; set; }
        public string operation { get; set; }
        public string folder { get; set; }
        public string workingDirectory { get; set; }
        public string fileToken { get; set; }
    }

    //-----------------------------------------------------------------------------------------
    public class CreateExamJob : BackgroundJob<CreateExamJobArgs>, ITransientDependency, IApplicationService
    {
        //=========================================================================================
        #region Private Fields
        //-----------------------------------------------------------------------------------------
        private readonly ITempFileCacheManager _tempFileCacheManager;
        private readonly IDbContextProvider<DbContext> _dbContextProvider;
        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly ISettingManager _settingManager;
        private readonly ILocalizationManager _localizationManager;
        private readonly IAbpSession _abpSession;
        private readonly IRepository<BinaryObject, Guid> _binaryObjectRepository;
        private readonly IBinaryObjectManager _binaryObjectManager;

        //-----------------------------------------------------------------------------------------
        #endregion Private Fields
        //=========================================================================================
        //-----------------------------------------------------------------------------------------
        public CreateExamJob(
            ITempFileCacheManager tempFileCacheManager,
            IDbContextProvider<DbContext> dbContextProvider,
            ISettingManager settingManager,
            IWebHostEnvironment hostingEnvironment,
            ILocalizationManager localizationManager,
            IRepository<BinaryObject, Guid> binaryObjectRepository,
            IBinaryObjectManager binaryObjectManager,
            IAbpSession abpSession
        )
        {
            //Debug.WriteLine("CreateExamJob.CreateExamJob()");
            _dbContextProvider = dbContextProvider;
            _settingManager = settingManager;
            _hostingEnvironment = hostingEnvironment;
            _tempFileCacheManager = tempFileCacheManager;
            _localizationManager = localizationManager;
            _binaryObjectRepository = binaryObjectRepository;
            _binaryObjectManager = binaryObjectManager;
            _abpSession = abpSession;
        }
        //----------------------------------------------------------------------------------------
        [UnitOfWork]
        public override void Execute(CreateExamJobArgs args)
        {
            try
            {
                var _all = _binaryObjectRepository.GetAllList();
                var alllist = _binaryObjectRepository.GetAllList(bo => bo.TenantId == 1);
            }
            catch (Exception ex)
            {
                ExceptionUtility.LogException(args.userId, ex);
            }
        }
        //-----------------------------------------------------------------------------------------
    }
}

4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Hi

    what's TenantId of _abpSession in the Execute method? We don't filters the TenantId in the repository query, framework will automatically filter by current context TenantId.

    var _all = _binaryObjectRepository.GetAllList();
    var alllist = _binaryObjectRepository.GetAllList(bo => bo.TenantId == 1);
    
  • User Avatar
    0
    TimMackey created

    I posted the wrong code. Here is the correct code:

    public override void Execute(CreateExamJobArgs args)
    {
            var guid = new Guid("02729c9d-dae0-683b-1d8f-39f5844c75e9");
            var file = GetBinaryObject(guid);
    }
    
    private async Task<byte[]> GetBinaryObject(Guid guid)
    {
        var file = await _binaryObjectManager.GetOrNullAsync(guid);
        if (file == null)
        {
            return null;
        }
        return file.Bytes;
    }
    
    

    Data in AppBinaryObjects: 02729c9d-dae0-683b-1d8f-39f5844c75e9 1 0x89504E470D0A1A0A0000000D494844520000008C000000A00806000000DEAE8473000000017352474200AECE1CE90000000467414D410000B18F0BFC61050000FDDA49444154785E4CFD65771C5BA22D0AFADD738A379B6531332B95CCCC99CA14A698999965C924C96C4B6690999937D7DEB58B0EBC7BEE7D6FF4A7EE0F3D46

    When _binaryObjectManager.GetOrNullAsync(guid) is invoked in an AppService, a BinaryObject is returned, as expected:

    _binaryObjectManager = {ngTTM.Storage.DbBinaryObjectManager}
    _binaryObjectRepository = {Castle.Proxies.IRepository`2Proxy_17}
    file = {[BinaryObject 02729c9d-dae0-683b-1d8f-39f5844c75e9]}
    

    When invoked in a Background Job, a null object is returned, unexpected:

    _binaryObjectManager = {ngTTM.Storage.DbBinaryObjectManager}
    _binaryObjectRepository = {Castle.Proxies.IRepository`2Proxy_17}
    file = null
    
  • User Avatar
    0
    maliming created
    Support Team

    hi

    I think the problem is because the TenantId is different from background job and AppService.

    You can consider pass the TenantId in the CreateExamJobArgs and switch the tenant in the Execute method.

  • User Avatar
    0
    TimMackey created

    Your suggestion is the solution. Thank you.

    private async Task<byte[]> GetBinaryObject(Guid guid)
    {
    	using (CurrentUnitOfWork.SetTenantId(1))
    	{
    		var file = await _binaryObjectManager.GetOrNullAsync(guid);
    		if (file == null)
    		{
    			return null;
    		}
    		return file.Bytes;
    	}
    }