![](/img/trans.png)
[英]System.ObjectDisposedException: Cannot access a disposed object in Xamarin
[英]System.ObjectDisposedException: Cannot access a disposed object
我有以下GenericRepository:
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
public readonly SportsStore2Context Context;
protected DbSet<T> DbSet;
public GenericRepository(SportsStore2Context context)
{
Context = context;
DbSet = context.Set<T>();
}
public async Task<T> Get<TKey>(Expression<Func<T, bool>> filter = null, string includeProperties = "")
{
IQueryable<T> query = Context.Set<T>();
query = IncludePropertiesQuery(query, includeProperties);
if (filter != null)
{
query = query.Where(filter);
}
return await query.SingleOrDefaultAsync();
}
public async Task<List<T>> GetAll(Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "")
{
IQueryable<T> query = Context.Set<T>();
query = IncludePropertiesQuery(query, includeProperties);
if (orderBy != null)
{
query = orderBy(query);
}
var collection = await query.ToListAsync();
return collection;
}
public async Task Add(T entity, Expression<Func<T, bool>> filter = null)
{
var existing = await Get<T>(filter);
if (existing == null)
{
Context.Set<T>().Add(entity);
Save();
}
}
public void Update(T entity)
{
Context.Set<T>().Update(entity);
Save();
}
public void Delete(T entity)
{
var dbSet = Context.Set<T>();
if (Context.Entry(entity).State == EntityState.Detached)
{
dbSet.Attach(entity);
}
dbSet.Remove(entity);
Save();
}
private void Save()
{
try
{
Context.SaveChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private IQueryable<T> IncludePropertiesQuery(IQueryable<T> query, string includeProperties = "")
{
includeProperties = includeProperties.Trim() ?? string.Empty;
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
return query;
}
}
Get和GetAll工作正常,但是,當我嘗試向數據庫中添加內容時,出現“ System.ObjectDisposedException:無法訪問已處置的對象”錯誤。
我已經在“啟動配置”中聲明了存儲庫,如下所示:
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
可能是什么問題呢? 我是在錯誤地聲明上下文嗎?
感謝您的幫助和時間。
UPDATE
刪除await(async)可以正常工作
public void Add(T entity, Expression<Func<T, bool>> filter = null)
{
var existing = Get<T>(filter);
if (existing.Result != null) return;
Context.Add(entity);
Save();
}
這個對嗎?
(我不知道這個特定的類,但這是混合異步和非異步代碼的一般建議)
建議使用異步函數遵循命名模式MyFuncAsync是有充分理由的,這是因為當您嘗試從非異步函數調用異步函數時,它很容易看到。
從您的評論中刪除,這就是您所說的添加
public bool Add(T entity, Expression<Func<T, bool>> filter = null)
{
try
{
genericRepository.Add(entity, filter);
}
catch (Exception e)
{
return false;
}
return true;
}
但是您是在這里從非異步函數中調用異步函數(如果將該函數稱為AddAsync會更明顯),這將導致問題,除非您像這樣阻塞非異步函數:
public bool Add(T entity, Expression<Func<T, bool>> filter = null)
{
try
{
genericRepository.Add(entity, filter).Wait();
}
catch (Exception e)
{
return false;
}
return true;
}
最好一直保持異步狀態,因為該線程將在操作完成時阻塞,但這應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.