Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

BackgroundJob throw InvalidOperationException #2817


User avatar
0
goldstar25 created

Hi, When I use IRepository in BackgroundJob on**.NET Core**,It’s throw this exception: 发生了 System.InvalidOperationException HResult=0x80131509 Message=The operation cannot be completed because the DbContext has been disposed. Source=EntityFramework StackTrace: 在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 在 System.Data.Entity.Internal.Linq.InternalQuery1.GetEnumerator() 在 System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator() 在 System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() 在 System.Collections.Generic.List1..ctor(IEnumerable1 collection) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) 在 Magicodes.WeChat.Application.BackgroundJob.SyncWeChatUsersJob.Execute(Int32 args) 在 E:\workspace\Magicodes.Admin.Core\plus\Magicodes.WeChat\Magicodes.WeChat.Application\BackgroundJob\SyncWeChatUsersJob.cs 中: 第 26 行 hope to get your support!


4 Answer(s)
  • User Avatar
    0
    goldstar25 created

    Source code:[attachment=0:3g29fpyl]SyncWeChatUsersJob.zip[/attachment:3g29fpyl] SyncWeChatUsersJob.zip

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I didn't check the source code yet but you need to create a unitOfWork in your background job manually. You can do it like in this example <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#iunitofworkmanager">https://aspnetboilerplate.com/Pages/Doc ... orkmanager</a>.

    Please let us know if it does not work.

    Thanks.

  • User Avatar
    0
    goldstar25 created

    It's OK!Thanks! And need add these code to set tenantId filter:

    CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, tenantId);
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Since you start unitOfWok manually, you can swtich to any tenant using

    using (_unitOfWorkManager.Current.SetTenantId(tenantId))
    {
      // code for tenant
    }
    

    See <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy#switching-between-host-and-tenants">https://aspnetboilerplate.com/Pages/Doc ... nd-tenants</a>