使用的await任务< T>异步挂起控制器MVC应用程序 [英] Using await Task<T> async hangs controller in MVC app

查看:148
本文介绍了使用的await任务< T>异步挂起控制器MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个MVC的网站。站点1有一个控制器,它具有以下code调用网站2

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;
    }
}

我能够直接浏览的网址,看到了JSON ..但什么是从我的同行在网站上下载MVC JSON正确的方法是什么?

I'm able to directly browse the URL and see the JSON.. but what is the correct way to download JSON from my peer website in MVC?

推荐答案

您也许应该把控制器方法到异步方法,并使用等待避免死锁

You should probably turn the controller method into an 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
}

微软的ASP.NET教程中包括的异步方法在ASP.NET MVC 4

这篇关于使用的await任务&LT; T&GT;异步挂起控制器MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆