Ticket T1219373
Visible to All Users

Re: What is the recommended way to handle async operations in ViewControllers?

created a year ago

Hi,

This is a response to https://supportcenter.devexpress.com/ticket/details/t1182144
which is marked as duplicate of https://supportcenter.devexpress.com/ticket/details/t1178668

However, that ticket is marked private and can not be opened.

Anyway, it is sync / async discussion with Dennis finishing with:

Still, we are using async programming ourselves in our products where it makes sense or necessary (for instance, in Blazor where it is
enforced by the platform itself).

And this is exactly the culprit - Blazor.
Platform forces async, but XAF does not support it in view controllers with async methoed overrides which can lead to severe issues.

For instance, I have custom property editor which uses JavaScript (which uses async based pdf.js) control and is communicating with Blazor using JSInterop to get back the data - and JSInterop is async only, there is not even an option to run JSInterop in sync mode.

Control is designed to load PDF file and to receive a bitmap stamp and then allow user to resize and reposition the stamp - each move and resize is a singular property changed event which would cause a demanding process in the background (merge PDF with bitmap, seralize and return it as base64 string) - so I thought I would only mark objecspace as modified in those events and perform actual data retrieval in ObjectSpace_Committing event (basically, when user clicked Save button).

And this is where I am stuck now because I need to call async method in Objectspace_Committing which retrieves data from JavaScript component using JSInterop.

If I mark ObjectSpace_Committing as async - it does not work because ObjectSpace is disposed when method gets executed.
Any other method (Task.Run, TaskFactory, .Result, .GetAwaiter) causes deadlock.

XAF Blazor really needs full async support since platform enforces it.

Regards,
Mario

Show previous comments (5)

    Hi Dennis,

    Just to give you one more data point: Our apps also frequently interact with external services, all async (e.g. load/send some data via REST or a just a long running SQL query). Took me a while to figure out how to do that the best way in XAF Blazor with the given architecture. I would also be happy to have better built-in support for this.

    Regards,
    Piero

    Dennis Garavsky (DevExpress) 9 months ago

      Thank you for your feedback, Piero. Please share your current solution with other XAFers here, if possible (just a code snipped or attach your full Controller code).

        Viewcontrollers should have async support.

        For example:

        • ExecuteAsync
        • OnActivateAsync
        • OnDeactivatedAsync
        • Other…

        Answers approved by DevExpress Support

        created 4 months ago (modified 3 months ago)

        Hello,

        I'm writing this follow-up to let you know that in v24.2 you can safely execute code from a background thread. The new BlazorApplication.InvokeAsync method will help you avoid concurrency issues in XAF Blazor apps:

        C#
        Task.Run(async () => { await blazorApplication.InvokeAsync(async () => { await Task.Delay(1000); View.ObjectSpace.Refresh(); Application.ShowViewStrategy.ShowMessage("Data refreshed"); }); });

        For more information, review the docs with code examples at InvokeAsync | Refresh Data in a View After a Period of Time (ASP.NET Core Blazor).

        Best regards,
        Herman

          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.