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.InternalSet
1.GetEnumerator()
在 System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() 在 System.Collections.Generic.List
1..ctor(IEnumerable1 collection) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable
1 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)
-
0
Source code:[attachment=0:3g29fpyl]SyncWeChatUsersJob.zip[/attachment:3g29fpyl] SyncWeChatUsersJob.zip
-
0
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.
-
0
It's OK!Thanks! And need add these code to set tenantId filter:
CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, tenantId);
-
0
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>