简体   繁体   English

如何使用Web api2在ASP.NET MVC中制作异步和等待方法

[英]How to make async and await method in asp.net MVC with Web api2


I'm newly to the ASP.NET MVC with Web API 2 and I created async and await method by using asp.net MVC with Web API 2 which is going to be store into the SQL DB. 我刚接触带有Web API 2的ASP.NET MVC,并且通过将带有Web API 2的asp.net MVC与ASP.NET MVC一起创建了异步和等待方法,该方法将存储到SQL DB中。 When I try to call my repository method in the API Controller I got an error cannot await ”system.collections.generic.list”. 当我尝试在API Controller中调用存储库方法时,出现错误,无法等待“ system.collections.generic.list”。 If anybody has idea about it kindly let me know. 如果有人对此有想法,请告诉我。
Note:- I don't want to use entity framework instead I want to store data through stored procedure. 注意:-我不想使用实体框架,而是想通过存储过程存储数据。

 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; } } 

在此处输入图片说明

The main problem is that you can't await a method that is not async. 主要问题是您无法等待不异步的方法。 You should rewrite your GetAllProduct method to make it async with the following signature: 您应该重写GetAllProduct方法以使其与以下签名异步:

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

Also don't forget to use Async methods to get your data from database: 另外,不要忘记使用Async方法从数据库获取数据:

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

As the previous answer said you have to change your signature to: 如先前的回答所述,您必须将签名更改为:

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

If you want to skip the ADO.NET boilerplate stuff, you can instead use Dapper ( https://github.com/StackExchange/dapper-dot-net ) to simplify the code: 如果要跳过ADO.NET样板文件,可以改用Dapperhttps://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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net 内核中的异步等待 web api(不是 MVC) - async-await in asp.net core web api (not MVC) Asp.Net Web Api2 + SignalR - Asp.Net Web Api2 + SignalR 如何在ASP.NET Web API2中自定义响应 - How to custom response in asp.net web api2 DelegatingHandler应该如何进行异步调用(ASP.NET MVC Web API)? - How should a DelegatingHandler make an async call (ASP.NET MVC Web API)? ASP.NET 内核 Web API - 如何解决警告 CS1998 此异步方法缺少“等待”运算符,将同步运行 - ASP.NET Core Web API -How to resolve Warning CS1998 This async method lacks 'await' operators and will run synchronously 关于ASP.NET Web API - 异步和等待 - About ASP.NET Web API - Async and Await 有效地使用ASP.NET Web API的async / await - Effectively use async/await with ASP.NET Web API 是否可以在特定控制器中使用任意中间件,或者在ASP.NET Web API2中使用过任何方法? - Is it possible to use arbitrary middleware in specific controller or ever method in asp.net web api2? 将Autofac与Mediatr ASP.NET Web API2集成 - Integrate Autofac with mediatr ASP.NET Web API2 如何在ASP.NET Web API中使用非线程安全的异步/等待API和模式? - How to use non-thread-safe async/await APIs and patterns with ASP.NET Web API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM