Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page loading takes forever

Tags:

c#

asp.net-mvc

I have a simple method:

public async Task<List<Steam>> Get()
{
    ObjectResult result = new Models.ObjectResult();
    using (HttpClient client = new HttpClient())
    {
        var Object = await client.GetAsync("http://api.steampowered.com/ISteamApps/GetAppList/v0001/");
        if (Object != null)
        {
            JObject steamobject = JObject.Parse(Object.ToString());
            var item = steamobject.SelectToken("applist").SelectToken("apps");
            result = JsonConvert.DeserializeObject<ObjectResult>(item.ToString());
        }

    }
    return result.MyList;
}

in my Index.cshtml :

        SteamGet getter = new SteamGet();
        List<Steam> Games = getter.Get().Result;
        foreach (var item in Games)
        {
           <li>
               @item.Name
           </li>
        }

This makes me wait forever!

like image 883
Greta Porretta Avatar asked Apr 18 '17 12:04

Greta Porretta


People also ask

Why does Page take forever to load?

A large volume of unoptimized images is usually the most common reason behind website slowness. High-resolution images can consume lots of bandwidth while loading. Uploading larger sized images and then scaling them down can unnecessarily increase the size of your web page – causing your website to load slowly.

How do I fix slow loading pages?

If your website is loading slow, you can speed it up by using faster hosting, page builders, plugins, and images. Configuring a caching solution and CDN should also help, plus optimizing third party scripts like Google Fonts. Finally, make sure to clean you database and use PHP 7.4.


1 Answers

This is a deadlock situation.

You're using .Result which blocks the current thread and waits for response - while in the async method , after finished — it tries to get back to that thread but that thread is already blocked ( by your .Result).

You should async/await all the way (or use ConfigureAwait(false))

like image 85
Royi Namir Avatar answered Sep 24 '22 19:09

Royi Namir