Base solution for your next web application
Open Closed

Hangfire Integration #11765


User avatar
0
alliance225 created

Hello,

I used this https://aspnetboilerplate.com/Pages/Documents/Hangfire-Integration Hi get this error on Hangfire dashboard

An exception occurred during performance of the job. Abp.Authorization.AbpAuthorizationException (desktop-5bai7bg:34012)

L'utilisateur courant ne peut pas s'authentifier à l'application!

Abp.Authorization.AbpAuthorizationException: L'utilisateur courant ne peut pas s'authentifier à l'application! at Abp.Authorization.AuthorizationHelper.AuthorizeAsync(IEnumerable`1 authorizeAttributes) at Abp.Authorization.AuthorizationHelper.CheckPermissionsAsync(MethodInfo methodInfo, Type type) at Abp.Authorization.AuthorizationHelper.AuthorizeAsync(MethodInfo methodInfo, Type type) at Abp.Authorization.AuthorizationInterceptor.InternalInterceptAsynchronous(IInvocation invocation) at SendNotificationJob.SendTestNotification() in xxxxl\src\DocuPro.Application\HelperControllers\NotificationBase.cs:line 61 at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Here is my method:

using Abp.Configuration.Startup; using Abp.Domain.Repositories; using Abp.Runtime.Session; using Abp; using DocuPro.Authorization.Users; using DocuPro.Notifications; using System.Threading.Tasks; using System.Linq; using Abp.Domain.Uow; using System;

public class SendNotificationJob { private readonly INotificationAppService _notificationAppService; private readonly IAbpSession _abpSession; private readonly IRepository<User, long> _userRepository; private readonly UserManager _userManager; private readonly IMultiTenancyConfig _multiTenancyConfig; private readonly IUnitOfWorkManager _unitOfWorkManager; public SendNotificationJob( INotificationAppService notificationAppService, IAbpSession abpSession, IRepository<User, long> userRepository, UserManager userManager, IMultiTenancyConfig multiTenancyConfig, IUnitOfWorkManager unitOfWorkManager) { _notificationAppService = notificationAppService; _abpSession = abpSession; _userRepository = userRepository; _userManager = userManager; _multiTenancyConfig = multiTenancyConfig; _unitOfWorkManager = unitOfWorkManager; }

public async Task SendNotifications()
{

    using (var unitOfWork = _unitOfWorkManager.Begin())
    {
        try
        {
           
            var users = _userRepository.GetAll().ToList();

            foreach (var user in users)
            {
                var userIdentifier = new UserIdentifier(1, user.Id);
                var message = "Your New Hangfire notification message"; // Replace with your message

                await _notificationAppService.SendNotificationUsingHangfireAsync(userIdentifier, message);
            }

            // Commit the unit of work if everything is successful
            unitOfWork.Complete();
        }
        catch (Exception ex)
        {
            // Handle exceptions or roll back the unit of work if necessary
            unitOfWork.Dispose();
            throw;
        }
    }
  
}

}


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

    Hi @alliance225,

    Is this the full stacktrace ? If not, could you share the full stacktrace ?

    I assume the method below (or the appService) requires an auhenticated user. So, if you want to use this method in a background job, you can;

    1. Move SendNotificationUsingHangfireAsync to a domain service and use it in NotificationAppService and your background job.
    2. Or, you can remove authorication for this specific method (not suggested)
    3. Or, you can operate this method by imitating a user (this is also not suggested)
    await _notificationAppService.SendNotificationUsingHangfireAsync(userIdentifier, message);
    

    So, I assume you can use option 1 and the problem should be resolved after that.

  • User Avatar
    0
    alliance225 created

    Thanks for the clear and thorough answer. Chose option 1 and it worked.