我花了几个小时试图弄清楚(标题)。所以我有两种方法:public virtual TEntity GetByName(Expression<Func<TEntity, bool>> whereName) { return this.DbEntitySet.FirstOrDefault(whereName);; } 和public virtual async Task<TEntity> GetByNameAsync(Expression<Func<TEntity, bool>> whereName) { try { return await this.DbEntitySet.FirstOrDefaultAsync(whereName); } catch (AggregateException ae) { throw; } catch (Exception ex) { throw; } } 第一种方法按预期工作,但第二种方法如果找到则运行正常,但如果没有找到则进入永无止境的进程。我预计会抛出异常(可能通过AggregateException),即使它没有抛出 TimeOutException。我试图修改它,如:public virtual async Task<TEntity> GetByNameAsync(Expression<Func<TEntity, bool>> whereName) { try { TimeSpan ts = TimeSpan.FromMilliseconds(5000); Task<TEntity> task = this.DbEntitySet.FirstOrDefaultAsync(whereName); if (!task.Wait(ts)) { throw new TimeoutException(); } return await task; } catch (AggregateException ae) { throw; } catch (Exception ex) { throw; } }然后这个方法运行正常WITHOUT ANY exception。请问有什么帮助吗??
1 回答
- 1 回答
- 0 关注
- 165 浏览
添加回答
举报
0/150
提交
取消