Q: How do I get started with asynchronous operations in general and their XPO use in particular?
A: Check out the Asynchronous Programming Patterns and Task-based Asynchronous Pattern articles in Microsoft .NET Guide. To get started with XPO asynchronous APIs specifically, consider the following resources:
- see the XPO - Async/Await Method Support (v18.2) blog post;
- download our GitHub demo;
- review our XpoTutorials demo files ("c:\Users\Public\Documents\DevExpress Demos 18.2\Components\WinForms\CS\XpoTutorials\AsyncLoadingTaskBased\AsyncLoadingTaskBased.cs" );
- consult with the Session, XPQueryExtensions, and other key API references.
Q: Do all ADO.NET providers support async operations and how XPO deals with it?
A: Not all ADO.NET providers supported by XPO support asynchronous operations. Also, even though XPO can pass System.Threading.CancellationToken from XPO async APIs to the underlying ADO.NET provider APIs, certain providers can ignore this token. To force XPO to always wrap synchronous methods in Task.Run calls, you can set the static XpoDefault.DataStoreAsyncBehavior property to WrapAlways. This can be used in desktop apps to create a non-blocking pseudo-asynchronous UI for providers like Oracle that do not natively implement async methods yet. Do not use this approach in Web apps.
Q: Do these new Task-based APIs support cancellation?
A: All XPO methods accept System.Threading.CancellationToken. For more information on how to cancel async operations with XPO, refer to the attached XpoAsyncCancel.zip project (research its Form1.cs file).
Q: Do these new Task-based APIs provide progress notifications?
A: We do not provide any means for reporting progress by the following reasons:
- Standard asynchronous APIs (IDbConnection, IDbCommand, IDataReader, etc.) of ADO.NET drivers supported by XPO do not support IProgress<T> or similar means.
- Even if we had provided XPO method overloads accepting the IProgress<T> parameters, it would not be much usable: the progress would stay at 0% most of the method execution time and then immediately go to 100%. For instance, 0%...0%...0%...0%...0%...99%...100%. That is because the most heavy operations are done at the database level and their progress could not be reliably determined.
Q: Can I use async/await with direct SQL queries (Session methods like ExecuteScalar, ExecuteQuery, GetObjectsFromQuery, etc.)?
A: Yes, this is supported in v19.1+. Also, note that not all ADO.NET providers supported by XPO support asynchronous operations.
In v18.2, wrap these method calls into the Task.Run method manually, if required. Note that the asynchronous operation cannot be canceled under this approach.
Q: Can I use async/await with XPCollection and XPView?
A: No, XPCollection.LoadAsync and XPView.LoadAsync will not return Task and thus will not support async/await. We recommend that you use XPQuery<T> instead - it is a more efficient way to query data.
Q: Should we use the ThreadSafeDataLayer to use async/await XPO methods?
A: In general, the requirement to use the ThreadSafeDataLayer remains the same as if synchronous operations were used in the same context. If the existing code works with the SimpleDataLayer and you just change it to use asynchronous methods, you can continue using the SimpleDataLayer.
The only thing that should be avoided is the parallel execution of two asynchronous operations in the context of one Session instance. Use the await operator if you need to call an asynchronous method several times.[C#]
await session.DoSomethingAsync(); await session.DoSomethingAsync();
Q: Are there any plans to extend XAF's IObjectSpace to support async/await?
A: While we cannot provide any ETA, this XPO feature is definitely the first step in this direction. This support will bring XAF users the following benefits:
- improve XAF WinForms UX and perceived performance with the help of asynchronous Actions (open DetailView from ListView, save DetailView, execute long custom Actions);
- improve the new ASP.NET Core Data Service throughput under a high load.
For more information on this feature, please refer to the following support tickets: T112329, AS6060, CQ59274, Q352118.
Right away, you can use the IObjectSpace > GetObjectsQuery<T> method with ToListAsync, ToArrayAsync, CountAsync and other suitable IQueryable methods. Create, update and delete operations require us to extend IObjectSpace and then support these XXXAsync methods at the UI level. However, nothing prevents you from wrapping these IObjectSpace method calls into the Task.Run method manually, if required. Note that the asynchronous operation cannot be canceled under this approach.
Show all comments