i think you have to change your code in GetWaveRiderEdit(string waveRiderName)
. Line 4 needs await
keyword.
var model = await _waveRiderWrapper.GetWaveRider(waveRiderName).Result;
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
i think you have to change your code in GetWaveRiderEdit(string waveRiderName)
. Line 4 needs await
keyword.
var model = await _waveRiderWrapper.GetWaveRider(waveRiderName).Result;
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://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task?view=net-5.0