Ticket T621512
Visible to All Users

How to use DataSourceLoader in an async controller action

created 7 years ago

Hi

How to use async/await loading patterns (https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro#asynchronous-code) with DataSourceLoader?
I'll explain.

Here is a sample https://js.devexpress.com/Documentation/Guide/ASP.NET_MVC_Controls/Data_Binding
Specifically this part:
public object GetOrders(DataSourceLoadOptions loadOptions)
{
return DataSourceLoader.Load(_nwind.Orders, loadOptions);
}

We get a request, parse it with model binding and using DataSourceLoading process it (filter, sort, group, etc.).
The last part (loading) might take some considerable time because of database/network access.
I suppose async/await is used to accomodate for this and free some resources for processing with other threads while busy with this request.

But how to do this using DataSourceLoader? Or perhaps there are other ways?

We have a WebApi server which should process requests from client using DevExtreme components (like DataGrid).
I guess DataSourceLoadOptions works just for that, but it's not asynchronous.

One way to do it is to wrap the loading part into a Task.Start() and await for it, but it doesn't seems right to me, because using EF Core in other threads requires creation of new db context per thread.
ASP Net Core 2.0 has some caching for db context creations and if we just create a new one per request every time, this caching won't be used much.

Is there any way around this or i'm just missing something from the docs and we can safely wrap DataSourceLoader.Load() into a Task.Run() ?

Thanks.

Answers approved by DevExpress Support

created 7 years ago (modified 5 years ago)

Hi Sultan,

Updated:
Starting with version 2.5.0 we implemented the async API for the DataSourceLoader.Load method. Please refer to the Introduce async API GitHub issue for more information.

For earlier versions:

Attached is an example imitating an asynchronous controller action. See the OrdersController.cs file. Please test it and let us know your results.

If this does not help, feel free to modify the example to show the issue and send it back to us. We will examine it and put you on the right track.

    Show previous comments (4)
    KK KK
    Kasbolat Kumakhov 7 years ago

      Thank you for detailed response.

      I have also made some stress tests using Task.Run() pattern and can confirm, that it really works.
      Also, as a suggestion, it would be helpful for others if something like this was mentioned in the docs.
      Something like, "async/await pattern for EF can be freely replaced using Task.Run()".

      DevExpress Support Team 7 years ago

        Sultan,

        Thank you for your suggestion. We will think about adding this information to our docs.

        KK KK
        Kasbolat Kumakhov 5 years ago

          Just wanted to comment that this update (Async loading) works perfectly!
          Thanks!

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.