簡體   English   中英

'無法訪問已處理的對象。 此錯誤的一個常見原因是處理從依賴注入解決的上下文

[英]'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection

我將ThreadPool與通用存儲庫一起使用,但出現此錯誤;

'無法訪問已處理的對象。 此錯誤的一個常見原因是處理從依賴注入解析的上下文,然后嘗試在應用程序的其他地方使用相同的上下文實例。 如果您在上下文上調用 Dispose() 或將上下文包裝在 using 語句中,則可能會發生這種情況。 如果你使用依賴注入,你應該讓依賴注入容器來處理上下文實例。

private readonly AuthorizedServiceService _authorizedServiceService;
        private readonly CustomerService _customerService;
        public IConfigurationRoot Configuration { get; }

        public UpdateService(AuthorizedServiceService authorizedServiceService, CustomerService customerService)
        {
            _authorizedServiceService = authorizedServiceService;
            _customerService = customerService;
        }


        public void UpdateAllRecords()
        {

            _authorizedServiceService.GetByActive().ToList().ForEach(UpdateAuthorizedServiceRecords);
        }

        void UpdateAuthorizedServiceRecords(AuthorizedService authorizedService)
        {
            //UpdateStart(authorizedService);
            var mywatch = new Stopwatch();

            mywatch.Start();

            ThreadPool.QueueUserWorkItem(new WaitCallback(state => { UpdateStart(authorizedService); }));

            mywatch.Stop();
            mywatch.Reset();
        }


        public void UpdateStart(AuthorizedService authorizedService)
        {
            UpdateCustomers(authorizedService);
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => { UpdateCustomers(authorizedService); }));

        }

        internal void UpdateCustomers(AuthorizedService authorizedService)
        {
            try
            {
                if (authorizedService.CustomerUpdateLocked)
                    return;

                var carDatabaseClient = new DataCarDatabaseClient();
                var result = carDatabaseClient.GetCustomers(authorizedService, authorizedService.LastCustomerUpdate);

                var dataRows = Convert<Customer>(result).Select(s=>
                {
                    s.Id = authorizedService.Code + "-" + s.DcId;
                    s.AuthorizedService = authorizedService;
                    return s;
                }).ToList();

                _customerService.SaveOrUpdate(dataRows.OrderBy(p=>p.Id).FirstOrDefault(),p=> p.Id != null);

            }
            catch (Exception e)
            {
                // ignored
            }
        }

通用存儲庫方法;

public void AddOrUpdate(T entity, Expression<Func<T, bool>> predicate)
{
    var exists = predicate != null ? Context.Set<T>().Any(predicate) : Context.Set<T>().Any();
    if (!exists)
        Context.Set<T>().Add(entity);
    else
        Context.Set<T>().Update(entity);

    Context.SaveChanges();
}

發生這種情況是因為主線程中的所有依賴項在其執行完成並且您嘗試在另一個線程中訪問它們時都被處理掉了。 要處理這種情況,您需要在后台線程中創建一個范圍並在那里解析AuthorizedServiceService

private readonly IServiceScopeFactory _scopeFactory;

public UpdateService(AuthorizedServiceService authorizedServiceService, CustomerService customerService, IServiceScopeFactory scopeFactory)
{
    _authorizedServiceService = authorizedServiceService;
    _customerService = customerService;
    _scopeFactory = scopeFactory;
}

public void UpdateStart(AuthorizedService authorizedService)
{    
    ThreadPool.QueueUserWorkItem(new WaitCallback(state => { 
    using (scope = _scopeFactory.CreateScope())
    {
        var scopedAuthorizedService = scope.ServiceProvider.GetService(typeof(AuthorizedServiceService));
        UpdateCustomers(scopedAuthorizedService); }));
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM