Using wait Task <T> async hangs in controller in MVC application

I have two MVC websites. Site 1 has a controller that calls site 2 with the following code

// If I remove this in the controller of site1, then execution continues.... var asdf = SharedTypes.Utilities.GetjsonStream("http://localhost:11541/UIDP/Details/a1?format=json"); string g = asdf.Result; public class Utilities { public static async Task<string> GetjsonStream(string url) { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(url); string content = await response.Content.ReadAsStringAsync(); Debug.WriteLine("Content: " + content); return content; } } 

I can directly browse the URL and see JSON .. but what is the correct way to download JSON from my partner site in MVC?

+3
json c # asynchronous asp.net-mvc asp.net-mvc-4
source share
1 answer

You should probably include the controller method in the async method and use await to avoid deadlocks .

 public async Task<ActionResult> MyActionAsync() { var asdf = SharedTypes.Utilities.GetjsonStream(someUrl); string g = await asdf; // return something } 

The Microsoft ASP.NET Tutorial contains the async methods page in ASP.NET MVC 4 .

+2
source share

All Articles