is OData IQueryable async?

asp.net-core c# entity-framework-core odata

Question

I'm developing an OData endpoint to return a list of items from a database.

I'm returning an IQueryable and letting the front end handling the querying / filtering / expanding / paging through the odata query options.

Consider the following method:

[ODataRoute]
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public IActionResult Get() {
    return Ok(context.Vendors.AsQueryable());
}

I understand that async is not really needed, because the query is not being materialized here.

In this context, when is the query being materialized?

Is it being done in an asynchronous fashion?

1
2
6/24/2019 9:06:17 PM

Accepted Answer

You asked a few question Ill try to answer them all and clarify a few questions.

I understand that async is not really needed

Async is never really needed the question is will async benefit your application. E.g When a call is being made to the DbContext there is some network operations which take time, If you do not use async during that time a thread will be waiting for a response, instead of being return to the thread pool to handle other requests.

So yes, it's not needed but it could be useful.

In this context, when is the query being materialized?

The query is materialized (in the middleware) when the endpoint is accessed,

Is it being done in an asynchronous fashion?

No, When you pass the Queryable you are just deferring the execution until later this is still being done synchronously, you need to be using the async/await pattern for you to be doing something asynchronously

1
6/25/2019 1:52:26 PM

Popular Answer

Your query will be materialised during serialisation. When your method is accessed, to return the response in JSON, the serialiser tries to serialise the result. At that point, your query actually hits the database and result returned. The application might be slow because of no filters being applied and your query trying to retrieve all the records from the database.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow