后台工作器中的“unitOfWork 参数不能为空" [英] 'unitOfWork parameter cannot be null' in Background Worker

查看:42
本文介绍了后台工作器中的“unitOfWork 参数不能为空"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始收到这些错误.它在我以前的服务器上运行良好.

I've started getting these errors. It was working perfectly on my previous server.

using System;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Threading.BackgroundWorkers;
using EMS.IPs;
using System.Threading.Tasks.Dataflow;
using System.Threading.Tasks;
using System.Linq;
using EMS.Contacts;
using System.Collections.Concurrent;
using Abp.Domain.Uow;
using System.Collections.Generic;
using EMS.EmailValidation;
using Microsoft.AspNetCore.SignalR;
using KellermanSoftware.NetEmailValidation;
using System.Net;
using System.Collections;
using System.Threading;
using System.ComponentModel;
using System.Transactions;

namespace EMS.BackgroundWorkers
{
    public class ContactValidationBackgroundWorker : BackgroundWorkerBase, ITransientDependency
    {
        private readonly IRepository<IP> _ipsRepository;
        private readonly IRepository<Contact> _contactsRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IUnitOfWorkManager _unitOfWorkManager2;
        private readonly IRepository<Contact> _contactRepository;

        private BackgroundWorker bgworker = new BackgroundWorker();

        ActionBlock<ValidationObject> workerBlock;

        public ContactValidationBackgroundWorker(
            IRepository<IP> ipsRepository,
            IRepository<Contact> contactsRepository,
            IUnitOfWorkManager unitOfWorkManager,
            IUnitOfWorkManager unitOfWorkManager2,
            IRepository<Contact> contactRepository)
        {
            _ipsRepository = ipsRepository;
            _contactsRepository = contactsRepository;
            _unitOfWorkManager = unitOfWorkManager;
            _unitOfWorkManager2 = unitOfWorkManager2;
            _contactRepository = contactRepository;

            bgworker.DoWork += Worker;
        }

        public override void Start()
        {
            base.Start();
            bgworker.RunWorkerAsync();
        }

        public override void Stop()
        {
            bgworker.DoWork -= Worker;
        }

        public void Worker(object sender, DoWorkEventArgs e)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
            {
                var contacts = _contactsRepository.GetAll().Where(x => !x.IsChecked);
                if (!contacts.Any())
                {
                    Logger.Debug("No contacts");
                    return;
                }

                workerBlock = new ActionBlock<ValidationObject>(
                  async (arg) => await Validate(arg),
                  new ExecutionDataflowBlockOptions
                  {
                      MaxDegreeOfParallelism = 5
                  });

                foreach (var contact in contacts)
                {
                    workerBlock.Post(new ValidationObject()
                    {
                        Contact = contact
                    });
                }

                unitOfWork.Complete();
            }

            Logger.Debug("End posting jobs to threads. Awaits results...");

            workerBlock.Complete();
            workerBlock.Completion.Wait();
        }

        public override void WaitToStop()
        {
            base.WaitToStop();
        }

        private async Task Validate(ValidationObject validationObject)
        {
            try
            {
                using (var contactUnitOfWork = _unitOfWorkManager2.Begin(TransactionScopeOption.RequiresNew))
                {
                    Contact contact = validationObject.Contact;
                    contact.IsChecked = true;

                    await _contactRepository.UpdateAsync(contact);
                    await contactUnitOfWork.CompleteAsync();
                }
            } catch (Exception ex) {
                Logger.Error(ex.ToString());
                throw;
            }
        }
    }

    public class ValidationResult
    {
        public ValidationResult()
        {
            IsValid = false;
            Message = "";
        }

        public string Message { get; set; }
        public bool IsValid { get; set; }
    }

    public class ValidationObject
    {
        public Contact Contact { get; set; }
    }
}

它在后台工作人员中工作;它以前有效.现在没有了.Contact 对象不为空.似乎要求我将 unitOfWork 参数添加到 Update 方法.请帮我解决这个问题.

It works inside background worker; it worked before. Now it doesn't. Contact object is not null. It seems to ask me to add unitOfWork parameter to Update method. Please help me figure this out.

ERROR 2017-12-26 12:02:34,768 [14   ] orkers.ContactValidationBackgroundWorker - System.ArgumentNullException: Value cannot be null.
Parameter name: unitOfWork
   at Abp.EntityFrameworkCore.Uow.UnitOfWorkExtensions.GetDbContext[TDbContext](IActiveUnitOfWork unitOfWork, Nullable`1 multiTenancySide)
   at Abp.EntityFrameworkCore.Repositories.EfCoreRepositoryBase`3.Update(TEntity entity)
   at Castle.Proxies.Invocations.IRepository`2_Update_8.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IRepository`1Proxy_3.Update(Contact entity)
   at EMS.BackgroundWorkers.ContactValidationBackgroundWorker.<Validate>d__21.MoveNext() in /Users/grinay/Projects/EMSBackend/src/EMS.Application/BackgroundWorkers/ContactValidationBackgroundWorker.cs:line 329

更新1

  ERROR 2017-12-27 05:31:34,500 [9    ] orkers.ContactValidationBackgroundWorker - System.ArgumentNullException: Value cannot be null.
Parameter name: unitOfWork
   at Abp.EntityFrameworkCore.Uow.UnitOfWorkExtensions.GetDbContext[TDbContext](IActiveUnitOfWork unitOfWork, Nullable`1 multiTenancySide)
   at Abp.EntityFrameworkCore.Repositories.EfCoreRepositoryBase`3.UpdateAsync(TEntity entity)
   at Castle.Proxies.Invocations.IRepository`2_UpdateAsync_8.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IRepository`1Proxy_3.UpdateAsync(Contact entity)
   at EMS.BackgroundWorkers.ContactValidationBackgroundWorker.<>c__DisplayClass23_0.<<Validate>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task)
   at Nito.AsyncEx.AsyncContext.Run(Func`1 action)
   at EMS.BackgroundWorkers.ContactValidationBackgroundWorker.Validate(ValidationObject validationObject)

推荐答案

您不能像在后台工作者中那样将 async 方法与 unitOfWork 一起使用.

You cannot use async methods with unitOfWork like that in background worker.

在这 5 行中进行更改:

Make the changes in these 5 lines:

public void Worker(object sender, DoWorkEventArgs e)
{
    // ...

    workerBlock = new ActionBlock<ValidationObject>(
      (arg) => Validate(arg), // This line #1
      new ExecutionDataflowBlockOptions
      {
          MaxDegreeOfParallelism = 5
      });

    // ...
}

private void Validate(ValidationObject validationObject) // This line #2
{
    try
    {
        using (var contactUnitOfWork = _unitOfWorkManager2.Begin(TransactionScopeOption.RequiresNew))
        {
            Contact contact = validationObject.Contact;
            contact.IsChecked = true;

            AsyncHelper.RunSync(async () => // This line #3
            {                               // This line #4
                await _contactRepository.UpdateAsync(contact);
                await contactUnitOfWork.CompleteAsync();
            });                             // This line #5
        }
    } catch (Exception ex) {
        Logger.Error(ex.ToString());
        throw;
    }
}

这篇关于后台工作器中的“unitOfWork 参数不能为空"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆