Blazor and Async Await

ICodeInTx 56 Reputation points
2021-01-16T14:31:45.847+00:00

I am building a Blazor (server) application. Everything works well as long as I don't try to await a method. Basically I have a screen with a button. The button click event calls a method and the method uses a library which has an async method. If I try to use this async method then the Blazor just seems to hang and not come back. It appears that once the async method is called that control is turned back over to the web UI and that is the end. Even though I'm saying await. Maybe this async/await isn't meant for Blazor, I don't know.

From my razor component the method (below) calls the async method in my library. I'm using the .Result from this methods

public WaveRiderEditDisplayModel GetWaveRiderEdit(string waveRiderName)
        {
            WaveRiderEditDisplayModel newModel = null;
            var model = _waveRiderWrapper.GetWaveRider(waveRiderName).Result;
            if (model != null)
            {
                newModel = new WaveRiderEditDisplayModel
                {
                    AccountNumber = model.AccountNumber,
                    AccountUsageAmount = model.AccountUsageAmount,
                    BrokerName = model.BrokerName,
                    CompanyName = model.CompanyName,
                    RmId = model.RmId,
                    Name = model.Name,
                    ScrapeAmount = model.ScrapeAmount,
                    Shutdown = model.Shutdown,
                };
            }

            return newModel;
        }

Here is the async method I'm callling.

        public async Task<WaveRiderCreate> GetWaveRider(string name)
        {
            //WaveRiderCreate response = null;

            //Setup the querystring
            string requestString = $"api/waverider/name/{name}";

            //Create the request to send
            var request = new RestRequest(requestString, Method.GET, DataFormat.Json);
            //request.AddJsonBody(state);

            //Get The Response
            var response = await Task.Run(() => _client.PostAsync<WaveRiderCreate>(request));

            return response;
        }

The execution in debug goes to line 13 of this async method and never goes to the next line to return the response. At this point the control has been turned back over to the screen which is spinning and waiting for some result.

I'm "light" on the async/await code so am I right that I should be using the .Result where I am? How can I make this better (and work) Thanks in adavnce.
Scott

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,479 questions
0 comments No comments
{count} votes

Accepted answer
  1. parthibanuthayakumar 76 Reputation points
    2021-01-18T21:39:29.07+00:00

    i think you have to change your code in GetWaveRiderEdit(string waveRiderName) . Line 4 needs await keyword.

    var model = await _waveRiderWrapper.GetWaveRider(waveRiderName).Result;
    

1 additional answer

Sort by: Most helpful
  1. parthibanuthayakumar 76 Reputation points
    2021-02-17T16:42:57.43+00:00

    yes you are correct . it is better to use async and await all the way .

    if you really do not want to await keyword. Line 4 wiithout await

     var Taskmodel =  _waveRiderWrapper.GetWaveRider(waveRiderName).Result;  
    

    Here Taskmodel will be an Task Object . So what you can do is

    Taskmodel.RunSynchronously();
    Taskmodel.Wait();
    if(Taskmodel.IsCompleted)
    {
    var model = Taskmodel.Result;
    }

    Above lines can be used between line 4 and line 5

    References :
    https://visualstudiomagazine.com/blogs/tool-tracker/2019/10/calling-methods-async.aspx

    https://video2.skills-academy.com/en-us/dotnet/api/system.threading.tasks.task?view=net-5.0

    0 comments No comments