I am making a Http.JsonAsync<MyType>("api/MyController/Users"); call which returns very quickly but then times out in 5 secs rather than giving me the data.

I have made other such calls that have worked fine, but something weird is going on.

I started from the standard weather service Blazor client template and all worked fine

I kick it off from a button event

<button [email protected]_click>LOAD!</button>

@functions {
private void load_click()
{
    try
    {
        Logger.Log($"about to call PopulateExistingUsers");

        var taskUsers = Http.GetJsonAsync<UsersPageData>("api/ShowerQ/Users");

        Logger.Log($"returned from async requesting users");
        if (!taskUsers.Wait(5000))
        {
            throw new Exception($"timeout!");
        }
        var users = taskUsers.Result;
        Logger.Log($"populated existing users ok.  Users.Count = {users.Users.Count()}");
    }
    catch (Exception e)
    {
        Logger.Log(e.ToString());
    }
}

Server side Controller code:

[HttpGet("Users")]
public UsersPageData GetUsers()
{

    try
    {
        var users = _repos.GetAllUsers().ToList();
        Log($"returned from GetALlUsers() with {users.Count} users");

        return new UsersPageData() {Users = users};
    }
    finally
    {
        Log($"aft6er returning from method {nameof(GetUsers)}");
    }
}

Logger.Log just does Console.Writeline, which goes to the browser console - very handy!

Expected: log output to console: "populated existing users ok. Users.Count = 3"

ACtual logs:

The output I'm getting is:

WASM: 09:31:26.49:about to call PopulateExistingUsers

WASM: 09:31:26.56:returned from async requesting users

WASM: 09:31:31.57:System.Exception: timeout!

WASM:   at ShowerQWeb2.Client.Pages.Index.load_click () [0x00048] in C:\Users\XXXX\Source\repos\ShowerQWeb2\ShowerQWeb2.Client\Pages\Index.razor:62 

and on server side (which is running on same machine so clocks are in sync apart from a hour)

10:31:26.68:returned from GetALlUsers() with 3 users

10:31:26.68:aft6er returning from method GetUsers

The chrome network debug shows whats being returned: response header:

Date: Mon, 13 May 2019 09:31:26 GMT
{"users":[{"id":1,"name":"David XX"},{"id":2,"name":"Sumith YY"},{"id":3,"name":"David ZZ"}]}

So, it looks like its getting stuck deserializing maybe?

1 Answers

3
DavidG On

You're probably getting stuck in a deadlock because you are abusing async. You shouldn't be calling .Wait() and .Result, use async code properly.

First make the method async and return a Task:

private async Task load_click()
{
    // snip
}

Then await your HTTP calls properly:

var users = await Http.GetJsonAsync<UsersPageData>("api/ShowerQ/Users");