【问题标题】:Trying to make a synchronous task into an asynchronous task试图将同步任务变成异步任务
【发布时间】:2016-01-26 00:28:18
【问题描述】:

我将 LINQ 与 EntityFramewwork 6 一起使用,并且我需要将大部分方法转换为异步任务。

但是,我无法弄清楚为什么在这两个特定场景中我会收到这些设计时编译消息。如果有人可以向我解释我需要做什么才能使任务异步,将不胜感激。

我要转换的第一类同步任务如下:

public List<Category> GetProjectsByCategoryID(Int16 categoryid)
        {
            try
            {
                using (YeagerTechEntities DbContext = new YeagerTechEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    var category = DbContext.Categories.Include("Projects").Where(p => p.CategoryID == categoryid).ToList();

                    return category;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

当我尝试将上述方法更改为异步任务时(见下文),我不知道该放置什么异步方法inbetween the "Include("Projects").(p"

public async Task<List<Category>> GetProjectsByCategoryID(Int16 categoryid)
        {
            try
            {
                using (YeagerTechEntities DbContext = new YeagerTechEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    var category = await DbContext.Categories.Include("Projects").(p => p.CategoryID == categoryid);

                    return category;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

如何将这种同步方法转换为异步方法?

public List<CustomerEmail> GetCustomerDropDownList()
        {
            try
            {
                using (YeagerTechEntities DbContext = new YeagerTechEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    var customers = DbContext.Customers.Select(s =>
                        new CustomerEmail()
                        {
                            CustomerID = s.CustomerID,
                            Email = s.Email
                        }).ToList();

                    return customers;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

【问题讨论】:

  • 你为什么要catch (Exception ex){ throw ex;}?即使throw ex; 之前有记录代码,您也应该永远只调用throw ex;,这样您就破坏了堆栈跟踪,并且从中一无所获。您应该只调用 throw; 以保留堆栈跟踪,或者调用 throw new SomeOtherException("Some descriptive text", ex) 以便 ex 将作为内部异常返回。
  • 谢谢,我会做出改变的......
  • Scott,如果我将其保留为“throw ex”,那不会回到调用方法,点击“catch”并处理 ex.Message 异常吗?
  • 那么在这个方法中根本不要catch或者使用throw;而不是throw ex;ex.StackTrace; 被替换为指向throw ex; 位置的新堆栈跟踪。如果您只使用throw 或者在引发错误的实际位置没有捕获所有堆栈跟踪点。阅读this SO question了解更多信息。
  • 知道了...谢谢。我将对代码进行更改....

标签: c# multithreading linq entity-framework


【解决方案1】:

你们非常亲近:

public async Task<List<CustomerEmail>> GetCustomerDropDownList()
{
    try
    {
        using (YeagerTechEntities DbContext = new YeagerTechEntities())
        {
            DbContext.Configuration.ProxyCreationEnabled = false;
            DbContext.Database.Connection.Open();

            var customers = await DbContext.Customers.Select(s =>
            new CustomerEmail()
            {
                CustomerID = s.CustomerID,
                Email = s.Email
            }).ToListAsync();

            return customers;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

注意 ToListAsync()。这将显式加载和解析您的查询,但将以异步方式执行此操作。

【讨论】:

  • 谢谢....没有设计时编译错误...但是,我确实需要将第一行如下: public async Task> GetCustomerDropDownList() It缺少“任务”属性。
  • @sagesky36 我更新了我的答案以保持一致。谢谢。
  • 通常我不会调用 MyCollection.ToList() (异步或其他方式),因为将评估集合并立即将集合放入内存中。你怎么能返回一个 IEnumberable / IQueryable (?) 而不是一个列表,这样我可以进一步查询,然后只执行它准备好使用。就好像我们缺少一个 ToAsyncEnumerable/ToAsyncQueryable 解决方案。
  • @DanAbdn 您的前提不正确。 IQueryable 返回类型没有什么可等待的,因为它只是一个尚未解析的表达式。您应该始终在较低级别仅返回 IQueryable&lt;T&gt;,并在您对表达式状态感到满意时等待您的回复以 .ToListAsync() 解决。换句话说,在同一个语句块中等待和解析,并在其他地方构建简单的可查询表达式。
  • @DanAbdn 我继续,requested 这个功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
相关资源
最近更新 更多