Base solution for your next web application
Open Closed

Background job calling app service does not update database with new record #10289


User avatar
0
Astech created

We have the following background job:

class BuildAgendaJob : ITransientDependency
{
	private readonly IAppNotifier _appNotifier;
	private readonly IAgendasAppService _agendasAppService;
	private readonly IDocumentsAppService _documentsAppService;
	private readonly IHubContext<BuildProgressHub> _buildProgressHub;
	private readonly IAbpSession _session;
	private readonly IRepository<Agenda> _agendaRepository;
	private readonly IUnitOfWorkManager _unitOfWorkManager;

	public BuildAgendaJob(IAppNotifier appNotifier,
							IAgendasAppService agendasAppService,
							IHubContext<BuildProgressHub> buildProgressHub,
							IAbpSession session,
							IRepository<Agenda> agendaRepository,
							IDocumentsAppService documentsAppService,
							IUnitOfWorkManager unitOfWorkManager)
	{
		_appNotifier = appNotifier;
		_agendasAppService = agendasAppService;
		_buildProgressHub = buildProgressHub;
		_session = session;
		_agendaRepository = agendaRepository;
		_documentsAppService = documentsAppService;
		_unitOfWorkManager = unitOfWorkManager;
	}

	public async Task BuildAgenda(BuildAgendaJobArgs args, CancellationToken token)
	{
		using (var uow = _unitOfWorkManager.Begin())
		{
			// Set tenant Id
			_unitOfWorkManager.Current.SetTenantId(args.TenantId);

			// Run the job with the same permissions as the person that started it
			using (_session.Use(args.TenantId, args.UserId))
			{
				try
				{
					await _agendasAppService.AddAgendaDocument(
						new AddAgendaDocumentDto
						{
							AgendaId = args.AgendaId,
							FileName = "Agenda.docx",
						});

					//Notify the UI
					await _buildProgressHub.Clients.All.SendAsync("AgendaBuildFinished" + args.AgendaId);

					// Notify the user
					AsyncHelper.RunSync(() => _appNotifier.AgendaBuiltAsync(args));
				}
				catch (OperationCanceledException)
				{
					await _buildProgressHub.Clients.All.SendAsync("AgendaBuildFinished" + args.AgendaId);
					return;
				}
				catch (Exception ex)
				{
					//TODO
				}
			}
		}
	}
}

<br> As you can see above, it calls:

_agendasAppService.AddAgendaDocument

Which inserts the new record into the database. It appears to be working as in that method it returns the next available id in the database after its insert. However, the database does not get updated. Is as if the code has inserted the record but it has not pushed the changes to EF/database.

I have attempted to add: <br>

await _unitOfWorkManager.Current.SaveChangesAsync();

at the end of the background job however the record does still not get pushed to the database.

Am I missing something? Usually the app service will push any changes to the dbcontext automatically. Why is this not the case here?

Thanks


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you try calling uow.CompleteAsync(); at the end of using statement ?

  • User Avatar
    0
    Astech created

    That's worked, many thanks @ismcagdas