【问题标题】:How to make async and await method in asp.net MVC with Web api2如何使用 Web api2 在 asp.net MVC 中制作异步和等待方法
【发布时间】:2016-08-29 11:19:04
【问题描述】:


我是带有 Web API 2 的 ASP.NET MVC 的新手,我通过使用带有 Web API 2 的 asp.net MVC 创建了 async 和 await 方法,该方法将存储到 SQL DB 中。当我尝试在 API 控制器中调用我的存储库方法时,我收到一个错误,无法等待“system.collections.generic.list”。如果有人对此有任何想法,请告诉我。
注意:- 我不想使用实体框架,而是想通过存储过程存储数据。

Model:

namespace AsyncMVCWEBAPI.Models
{
    public class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
        public string Category { get; set; }
    }
}

API Controller:

public static async Task<Product> GetProduct()
{
    return await GetAllProduct(); // Here i'm getting an error can not await "system.collections.generic.list"
}

Repository:

public static IEnumerable<Product> GetAllProduct()
{           
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        List<Product> students = new List<Product>();
        SqlCommand cmd = new SqlCommand("spGetAllStudentDetails", con);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Product product = new Product();
            product.Price = Convert.ToInt32(rdr["Price"]);
            product.Name = rdr["Name"].ToString();
            product.Category = rdr["Category"].ToString();

            students.Add(product);

        }
        return students;
    }           
}

【问题讨论】:

    标签: c# async-await asp.net-web-api2 c#-5.0 asp.net-mvc-5.1


    【解决方案1】:

    主要问题是您不能等待非异步方法。您应该重写您的 GetAllProduct 方法以使其与以下签名异步:

    public static async Task<IEnumerable<Product>> GetAllProduct()
    

    别忘了使用异步方法从数据库中获取数据:

        ...
        await con.OpenAsync();
        ...
        SqlDataReader rdr = await cmd.ExecuteReaderAsync();
        while (await rdr.ReadAsync())
        {
            ...
            students.Add(product);
        }
        return students;
    

    【讨论】:

    • 感谢您对 Ivan Yuriev 的精彩支持,它会正常工作的。我还有一个疑问,await con.OpenAsync() 和 con.Open()、rdr.ReadAsync() 和 rdr.Read()、await cmd.ExecuteReaderAsync() 和 await cmd.ExecuteReader() 有什么区别。请告诉我?
    • 这些方法帮助你使用 .net 4.5 的 async\await 特性 主要思想是简化编写异步代码(顺便说一下,异步不是多线程)。所以,在你等待的时候,你的主要工作线程是空闲的:网络请求、数据库调用等。而且你可以更有效地利用资源,你最好阅读官方手册:msdn.microsoft.com/en-us/library/hh191443.aspx
    • 感谢伊万·尤里耶夫。这非常有用。
    【解决方案2】:

    正如之前的答案所说,您必须将签名更改为:

    public static async Task<IEnumerable<Product>> GetAllProduct()
    

    如果您想跳过 ADO.NET 样板文件,您可以改用 Dapper (https://github.com/StackExchange/dapper-dot-net) 来简化代码:

    public static async Task<IEnumerable<Product>> GetAllProduct()
    {
        using (var con = new SqlConnection(connectionString))
        {
            await con.OpenAsync();
            return await con.QueryAsync<Product>("spGetAllStudentDetails", commandType: CommandType.StoredProcedure);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-12
      • 2018-07-21
      • 2018-05-14
      • 2014-05-15
      • 1970-01-01
      • 2021-05-21
      • 2017-10-08
      • 2015-02-03
      • 1970-01-01
      相关资源
      最近更新 更多