Base solution for your next web application
Open Closed

Hangfire exception using UnitOfWork #2089


User avatar
0
amayorquin created

Hi,

Can you help me please, I'mt trying to use hangfire to set a recurring job to run over all tenants. The job works fine when I use the sample you prepared: <a class="postlink" href="https://gist.github.com/hikalkan/d543b6ded179d1f05227f49bc70e2fbe">https://gist.github.com/hikalkan/d543b6 ... 9bc70e2fbe</a>

But when I tried to use UOW I,m getting an error. This is my code:

In Startup.cs in configuration method I just added this line at the end:

app.UseHangfireDashboard("/hangfire", new DashboardOptions());

In the web module I added this in PostInitialize Methdod

var jobs = IocManager.Resolve<BackgroundJobs>();
RecurringJob.AddOrUpdate("ReplyUpdates", () => jobs.ReplyUpdates(), Cron.MinuteInterval(5));

And my BackgroundJobs class:

public class BackgroundJobs : ITransientDependency
    {
        private readonly ILogger _logger;
        private readonly IRepository<Tenant> _tenantRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        public BackgroundJobs(ILogger logger,
            IRepository<Tenant> tenantRepository,
            IUnitOfWorkManager unitOfWorkManager)
        {
            _logger = logger;
            _tenantRepository = tenantRepository;
            _unitOfWorkManager = unitOfWorkManager;
        }

        [UnitOfWork]
        public virtual void ReplyUpdates()
        {
            var tenants = _tenantRepository.GetAllList(t => t.ConnectionString != null && t.ConnectionString != "");
            for (int i = 0; i < tenants.Count; i++)
            {
                var tenantId = tenants[i].Id;
                using (_unitOfWorkManager.Current.SetTenantId(tenantId))
                {
                    _logger.Info("MyJob1Class is working!");
                }
            }
            
        }
    }

When I added the [UnitOfWork] attributte I'm getting the following error, in the hangfire dashboard:

Could not load file or assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

If I not use the [UnitOfWork] attribute the Job runs fine, but _unitOfWorkManager.Current is null

Can you help me please

Thanks in advance


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

    Hi,

    Sorry for the late reply. You can check this issue for your problem. <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1296">https://github.com/aspnetboilerplate/as ... ssues/1296</a>

    I have tried the latest suggestion but right now only option seems like creating a manual unit of work.