Ticket T1251914
Visible to All Users
Duplicate

We have closed this ticket because another page addresses its subject:

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

How to use asynchronous methods in the OnLoggedOn method?

created 7 months ago

Hi,

I need to obtain the user's account and password when logging in to verify another system and perform API docking, but I need to save the obtained token in the session so that it can be saved when accessing the onloggedon method for the second time. But I found that no matter what method is used, it will cause the page to get stuck in the loading login interface.
Unless asynchronous methods are used instead of synchronous methods.

How can I wait for the asynchronous method to complete before executing base.onloggedon?

C#
protected override void OnLoggedOn(LogonEventArgs args) { AuthenticationStandardLogonParameters logonParameters = (AuthenticationStandardLogonParameters)args.LogonParameters; var sessionStorageService = ServiceProvider.GetRequiredService<ProtectedSessionStorage>(); var commonWeb = ServiceProvider.GetRequiredService<ICommonWeb>(); var principalProvider = ServiceProvider.GetRequiredService<IPrincipalProvider>(); //AzureAD if (AzureADAuthenticationProvider.CanHandlePrincipal(principalProvider.User)) { string sid = commonWeb.NasLogon_Azure(this); sessionStorageService.SetAsync("sid", sid); } //Password else { if (string.IsNullOrEmpty(logonParameters.UserName) == false && string.IsNullOrEmpty(logonParameters.Password) == false) { //string sid = commonWeb.NasLogon_Pwd(this, logonParameters.UserName, logonParameters.Password); //sessionStorageService.SetAsync("sid", sid); IsLoging = true; var httpclient = commonWeb.WebApiLogon(logonParameters.UserName, logonParameters.Password); sessionStorageService.SetAsync("webapi_token", httpclient); } } try { //using this code will stuck in the loading scress var protectedBrowserStorageResult = sessionStorageService.GetAsync<string>("sid").ConfigureAwait(false).GetAwaiter().GetResult(); if (protectedBrowserStorageResult.Success) { var httpClientFactory = this.ServiceProvider.GetRequiredService<IHttpClientFactory>(); string sid = protectedBrowserStorageResult.Value; FileStationHelper fileStationHelper = new FileStationHelper(httpClientFactory, sid, commonWeb.NAS_Admin_SID); commonWeb.NAS_SID = sid; commonWeb.FileServer = fileStationHelper; } var session_httpclient = sessionStorageService.GetAsync<string>("webapi_token").ConfigureAwait(false).GetAwaiter().GetResult(); if (session_httpclient.Success) { commonWeb.WebApiToken = session_httpclient.Value; } } catch (Exception e) { Console.WriteLine(e); throw; } finally { IsLoging = false; } //using this code will not stuck //Task.Run(async () => //{ // try // { // var protectedBrowserStorageResult = await sessionStorageService.GetAsync<string>("sid"); // if (protectedBrowserStorageResult.Success) // { // var httpClientFactory = this.ServiceProvider.GetRequiredService<IHttpClientFactory>(); // string sid = protectedBrowserStorageResult.Value; // FileStationHelper fileStationHelper = new FileStationHelper(httpClientFactory, sid, commonWeb.NAS_Admin_SID); // commonWeb.NAS_SID = sid; // commonWeb.FileServer = fileStationHelper; // } // var session_httpclient = await sessionStorageService.GetAsync<string>("webapi_token"); // if (session_httpclient.Success) // { // commonWeb.WebApiToken = session_httpclient.Value; // } // } // catch (Exception e) // { // Console.WriteLine(e); // throw; // } // finally // { // IsLoging = false; // } //}); base.OnLoggedOn(args); }

Answers approved by DevExpress Support

created 7 months ago

Hello.

//using this code will not stuck //
//Task.Run(async () =>

Yes, this solution is correct in your specific situation - let us know if you experience any issues.

In general, we have no special support for this type of scenario in the current XAF version and cannot offer alternative solutions, sorry. I elaborated more on this in What is the recommended way to handle async operations in ViewControllers? and Re: What is the recommended way to handle async operations in ViewControllers?, and we also keep tracking similar user requests (not many at this stage, so thank you for the sample). In certain cases, synchronous calls may help like in your case (Task.Run, TaskFactory, .Result, .GetAwaiter), but not in all the scenarios.

You can find additional information on handling other asynchronous operations in XAF Blazor in the following tickets:

    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.