Hello, I configured Hangfire and I have this job running: RecurringJob.AddOrUpdate<IMyDomainServices>( service => service.UpdateVentesAsync(), "*/2 * * * *"); // Cron expression for every 2 minutes
I have this in my module: public override void PreInitialize() { //Adding authorization providers Configuration.Authorization.Providers.Add<AppAuthorizationProvider>();
//Adding custom AutoMapper configuration
Configuration.Modules.AbpAutoMapper().Configurators.Add(CustomDtoMapper.CreateMappings);
IocManager.Register<IMyDomainServices, MyDomainServices>();
}
Here is my interface: namespace DocuPro.DomainServices { public interface IMyDomainServices: IDomainService { Task UpdateVentesAsync(); } }
And this is my Service:
using Abp.Authorization; using Abp.Domain.Repositories; using Abp.Domain.Uow; using Abp.Runtime.Session; using DocuPro; using DocuPro.Authorization.Users; using DocuPro.CommonBases; using DocuPro.DomainServices; using DocuPro.SortiesLocalesBases; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using System.Threading.Tasks;
public class MyDomainServices : DocuProAppServiceBase, IMyDomainServices { private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IRepository<StatsVente> _statsVenteRepository;
private readonly IRepository<SortiesLocales> _sortiesLocalesRepository;
public MyDomainServices(
IUnitOfWorkManager unitOfWorkManager,
IServiceScopeFactory serviceScopeFactory,
IRepository<StatsVente> statsVenteRepository,
IRepository<SortiesLocales> sortiesLocalesRepository)
{
_unitOfWorkManager = unitOfWorkManager;
_serviceScopeFactory = serviceScopeFactory;
_statsVenteRepository = statsVenteRepository;
_sortiesLocalesRepository = sortiesLocalesRepository;
}
[AbpAllowAnonymous]
public async Task UpdateVentesAsync()
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager>();
var abpSession = scope.ServiceProvider.GetRequiredService<IAbpSession>();
var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
var sortiesLocalesesAppService = scope.ServiceProvider.GetRequiredService<ISortiesLocalesesAppService>();
var periodesAppService = scope.ServiceProvider.GetRequiredService<IPeriodesAppService>();
var adminUser = await userManager.FindByNameAsync("admin");
if (adminUser == null)
{
throw new InvalidOperationException("Admin user not found.");
}
using (var unitOfWork = unitOfWorkManager.Begin())
{
using (abpSession.Use(adminUser.TenantId, adminUser.Id))
{
var periodeId = await GetLatestPeriodeId();
await sortiesLocalesesAppService.UpdateStatsVentesMois(periodeId);
}
await unitOfWork.CompleteAsync();
}
}
}
public async Task<int> GetLatestPeriodeId()
{
var usedPeriodeIds = await _statsVenteRepository
.GetAll()
.Select(sv => sv.PeriodeId)
.Distinct()
.ToListAsync();
var latestPeriodeId = await _sortiesLocalesRepository
.GetAll()
.Where(p => !usedPeriodeIds.Contains(p.PeriodeId)) // Exclude PeriodeIds that are in _statsVenteRepository
.OrderByDescending(p => p.PeriodeId) // Order by PeriodeId descending
.Select(p => p.PeriodeId)
.FirstOrDefaultAsync(); // Get the first (latest) PeriodeId that is not used
return latestPeriodeId; // This will be the latest PeriodeId not used in _statsVenteRepository
}
}
My problem is that any database call returns 0 rows even though there are data there.
As you can see I struggled with authorization, unitofwork, etc. to make it work.
What is the best way to do this?
1 Answer(s)
-
0
Hi @alliance225
I assume this is probalby because of background jobs work as host (TenantId=NULL) by default. Could you disable multi tenancy filter as shown here https://aspnetboilerplate.com/Pages/Documents/Data-Filters#disable-filters and see if you can get any data ?