I wrote this sync method that I want to run PostAsJsonAsync synchronously in it.
bool result2 = false;
HttpResponseMessage httpResponse2 = null;
void CallPostModel()
{
var task = Task.Run(() =>
{
Console.WriteLine("About to Request.");
var httpResponse2 = client.PostAsJsonAsync<PostTestModel>
("api/weatherforecast/PostTest",
new PostTestModel() { Id = 2, Name = "Michael" }).Result; //<< code executes until here
Console.WriteLine($"Ran Successfuly");
if (httpResponse2.IsSuccessStatusCode)
{
Console.WriteLine("Response Success.");
result2 = httpResponse2.Content.ReadFromJsonAsync<bool>().Result;
Console.WriteLine($"Result: {result2}");
return result2;
}
else
{
throw new Exception("Error from app. not success status code.");
}
});
}
With this method, Although the server method is called. but the result is not returned, and the server code executes and this Console.WriteLine($"Ran Successfuly"); is not executing.
So if anyone can correct this code or specify a way to do this in blazor webassembly, Run PostJsonAsync synchronously in a sync method?