System.ObjectDisposedException 在 ABP 中
System.ObjectDisposedException in ABP
我在核心层有以下class:
请求管理器:
public class RequestManager: MyCarParkServiceBase, ISingletonDependency
{
public static List<HitchRequest> GlobalHitchRequestList = new List<HitchRequest>();
public static List<CarparkRequest> GlobalCarparkRequestList = new List<CarparkRequest>();
public static Dictionary<CarparkRequest, List<HitchRequest>> GlobalRequestList = new Dictionary<CarparkRequest, List<HitchRequest>>();
private readonly IRepository<HitchRequest, long> _hitchRequestRepository;
private readonly IRepository<CarparkRequest, long> _carparkRequestRepository;
public RequestManager(IRepository<HitchRequest, long> hitchRequestRepository,
IRepository<CarparkRequest, long> carparkRequestRepository)
{
_hitchRequestRepository = hitchRequestRepository;
_carparkRequestRepository = carparkRequestRepository;
}
public void LoadAllActiveRequests()
{
//System.ObjectDisposedException Exception!!!!
_hitchRequestRepository.GetAll().Include(p => p.CarPark).Where(p => p.IsActive).ToList().ForEach(p => GlobalHitchRequestList.Add(p));
_carparkRequestRepository.GetAllList(p => p.IsActive).ForEach(p => GlobalCarparkRequestList.Add(p));
ManageRequestList();
}
}
我在尝试使用 GetAll() 方法时遇到以下错误:
System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.'
如果我改为使用 GetAllList() 并从中删除 Include(),则它可以正常工作。
我在 CoreModule class:
的 PostInitialize() 中调用了 LoadAllActiveRequests()
public override void PostInitialize()
{
IocManager.RegisterIfNot<IChatCommunicator, NullChatCommunicator>();
IocManager.Resolve<ChatUserStateWatcher>().Initialize();
IocManager.Resolve<RequestManager>().LoadAllActiveRequests();
IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
}
我注入了 UnitOfWorkManager 并将方法更改为以下方法并且成功了!
public void LoadAllActiveRequests()
{
using (var unitOfWork = _unitOfWorkManager.Begin())
{
_hitchRequestRepository.GetAll().Include(p => p.CarPark).Where(p => p.IsActive).ToList().ForEach(p => GlobalHitchRequestList.Add(p));
_carparkRequestRepository.GetAllList(p => p.IsActive).ForEach(p => GlobalCarparkRequestList.Add(p));
ManageRequestList();
}
}
仍然不太清楚为什么 [UnitOfWork] 属性对我的情况不起作用!
添加 [UnitOfWork]
属性并使其成为 virtual
方法:
[UnitOfWork]
public virtual void LoadAllActiveRequests()
{
// ...
}
参见:UnitOfWork Attribute Restrictions
You can use the UnitOfWork attribute for:
- All public virtual methods for self injected classes (Like MVC Controllers and Web API Controllers).
我在核心层有以下class:
请求管理器:
public class RequestManager: MyCarParkServiceBase, ISingletonDependency
{
public static List<HitchRequest> GlobalHitchRequestList = new List<HitchRequest>();
public static List<CarparkRequest> GlobalCarparkRequestList = new List<CarparkRequest>();
public static Dictionary<CarparkRequest, List<HitchRequest>> GlobalRequestList = new Dictionary<CarparkRequest, List<HitchRequest>>();
private readonly IRepository<HitchRequest, long> _hitchRequestRepository;
private readonly IRepository<CarparkRequest, long> _carparkRequestRepository;
public RequestManager(IRepository<HitchRequest, long> hitchRequestRepository,
IRepository<CarparkRequest, long> carparkRequestRepository)
{
_hitchRequestRepository = hitchRequestRepository;
_carparkRequestRepository = carparkRequestRepository;
}
public void LoadAllActiveRequests()
{
//System.ObjectDisposedException Exception!!!!
_hitchRequestRepository.GetAll().Include(p => p.CarPark).Where(p => p.IsActive).ToList().ForEach(p => GlobalHitchRequestList.Add(p));
_carparkRequestRepository.GetAllList(p => p.IsActive).ForEach(p => GlobalCarparkRequestList.Add(p));
ManageRequestList();
}
}
我在尝试使用 GetAll() 方法时遇到以下错误:
System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.'
如果我改为使用 GetAllList() 并从中删除 Include(),则它可以正常工作。 我在 CoreModule class:
的 PostInitialize() 中调用了 LoadAllActiveRequests()public override void PostInitialize()
{
IocManager.RegisterIfNot<IChatCommunicator, NullChatCommunicator>();
IocManager.Resolve<ChatUserStateWatcher>().Initialize();
IocManager.Resolve<RequestManager>().LoadAllActiveRequests();
IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
}
我注入了 UnitOfWorkManager 并将方法更改为以下方法并且成功了!
public void LoadAllActiveRequests()
{
using (var unitOfWork = _unitOfWorkManager.Begin())
{
_hitchRequestRepository.GetAll().Include(p => p.CarPark).Where(p => p.IsActive).ToList().ForEach(p => GlobalHitchRequestList.Add(p));
_carparkRequestRepository.GetAllList(p => p.IsActive).ForEach(p => GlobalCarparkRequestList.Add(p));
ManageRequestList();
}
}
仍然不太清楚为什么 [UnitOfWork] 属性对我的情况不起作用!
添加 [UnitOfWork]
属性并使其成为 virtual
方法:
[UnitOfWork]
public virtual void LoadAllActiveRequests()
{
// ...
}
参见:UnitOfWork Attribute Restrictions
You can use the UnitOfWork attribute for:
- All public virtual methods for self injected classes (Like MVC Controllers and Web API Controllers).