2

Here is my ODATA controller with an ODATA action.

   public class MyController : ODataController
{
    private readonly Repository_repo;

    public MyController(IRepository repo)
    {
        _repo = repo;
    }

    [EnableQuery(PageSize = 10, EnsureStableOrdering = false)]
    public IActionResult Get()
    {
        var data = _repo.GetData();
        return Ok(data)

    }

}

Here is my Repository method.

   public IQueryable<DataModel> GetData() => _db.DbSet.Select(data=> new DataModel
    {
        // Implement model
    }).

Now I understand there is no point making the GetData method in the repo as async because that is just returning a queryable which doesn't get executed till you call it's enumerator.

So the part to make async is the action method. How would I make this an async await- able call? Odata EnableQuery method expects an IQueryable as far as I am aware.

Foysal94
  • 565
  • 2
  • 7
  • 15

1 Answers1

1

You do not need to. The action method is just one part of the request pipeline; it not being async does not preclude other things in the pipeline from being async. The middleware that's processing the OData query is what is actually sending the query to the database, and very likely does so async (I'm not familiar with source code, and cannot say definitively). Regardless, your action need only be async if you're actually doing something async in it. Otherwise, don't worry about it.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • 1
    OData has no idea what ORM you use, if any. It only knows about your IQueryable, how would it know whether to use Dapper QueryAsync or EntityFrameworkCore ToListAsync, it can't, unless you tell it to. I checked the executed code and the database queries are not asynchronous, thereby blocking the thread. – Vqf5mG96cSTT Feb 10 '23 at 22:22