简体   繁体   中英

How to create a asynchronous method in asp.net web API

I need to get the data from my database using a web api. Below is the code on how I implement it.

[HttpGet]
[Authorize]
[Route("api/getproperties")]
public async Task<List<Property>> GetProperties()
{
    using(var db = new ApplicationDbContext())
    {
        var properties = await (from p in db.Properties
                        join pt in db.PropertyTypes
                        on p.PropertyTypeId equals pt.PropertyTypeId
                        select new
                        {
                            PropertyId = p.PropertyId,
                            PropertyName = p.PropertyName,
                            Owner = p.Owner,
                            Cluster = p.Cluster,
                            PropertyNumber = p.PropertyNumber,
                            RegionCode = p.RegionCode,
                            ProvinceCode = p.ProvinceCode,
                            MunicipalCode = p.MunicipalCode,
                            BarangayCode = p.BarangayCode,
                            DateAdded = p.DateAdded,
                            DateModified = p.DateModified,
                            PropertyTypeId = p.PropertyTypeId,
                            PropertyType = p.PropertyType,
                            Type = pt.Type
                        }
                                ).ToList()
                                .Select(x => new Property
                                {
                                    PropertyId = x.PropertyId,
                                    PropertyName = x.PropertyName,
                                    Owner = x.Owner,
                                    Cluster = x.Cluster,
                                    PropertyNumber = x.PropertyNumber,
                                    RegionCode = x.RegionCode,
                                    ProvinceCode = x.ProvinceCode,
                                    MunicipalCode = x.MunicipalCode,
                                    BarangayCode = x.BarangayCode,
                                    DateAdded = x.DateAdded,
                                    DateModified = x.DateModified,
                                    PropertyTypeId = x.PropertyTypeId,
                                    PropertyType = x.PropertyType,
                                    Type = x.Type
                                }).ToListAsync();

        return properties;
    }
}

If I do not use the "async Task>" and remove the ".ToListAsync()" and "await" at the beginning, I do not get the errors. But with it, the function is not going to be Asynchronous anymore. Can you please show me how to this right? Thank you.

When you use ToList() on IQueryable then asynchrony disappears since it loads data into memory synchronously.

Then you try to invoke ToListAsync on IEnumerable instead of IQueryable .

Your code should look like this:

public async Task<List<Property>> GetProperties()
{
    using(var db = new ApplicationDbContext())
    {
        var properties = await (from p in db.Properties
                          join pt in db.PropertyTypes
                          on p.PropertyTypeId equals pt.PropertyTypeId
                          select new Property()
                          {
                              PropertyId = p.PropertyId,
                              PropertyName = p.PropertyName,
                              Owner = p.Owner,
                              Cluster = p.Cluster,
                              PropertyNumber = p.PropertyNumber,
                              RegionCode = p.RegionCode,
                              ProvinceCode = p.ProvinceCode,
                              MunicipalCode = p.MunicipalCode,
                              BarangayCode = p.BarangayCode,
                              DateAdded = p.DateAdded,
                              DateModified = p.DateModified,
                              PropertyTypeId = p.PropertyTypeId,
                              PropertyType = p.PropertyType,
                              Type = pt.Type
                          }).ToListAsync();

        return properties;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM