Azure移动服务ListAsync永远挂起

时间:2015-06-01 19:55:03

标签: asp.net-mvc azure-mobile-services

如何从MVC同步调用移动服务表?

当我运行以下代码时,我的应用程序一直在等待结果:

public class HomeController : Controller
{
    MobileServiceClient mobileClient = new MobileServiceClient("My-Mobile-Server", "My-Key");

    public ActionResult Index()
    {
        var clientTable = mobileClient.GetTable<Clients>();
        var client = clientTable.ToListAsync().Result;

        if (client.Count == 0)
            ViewBag.TemCliente = "Não";
        else
            ViewBag.Cliente = client.First().Name;

        return View();
    }
}

有趣的是,我的单元测试中的代码完全相同。

如何同步列出我的移动服务表客户端?

1 个答案:

答案 0 :(得分:0)

看起来您遇到的问题是因为您没有将await与异步方法一起使用。

public class HomeController : Controller
{
    MobileServiceClient mobileClient = new MobileServiceClient("My-Mobile-Server", "My-Key");

    public ActionResult Index()
    {
        var clientTable = mobileClient.GetTable<Clients>();
        var client = await clientTable.ToListAsync().Result;

        if (client.Count == 0)
            ViewBag.TemCliente = "Não";
        else
            ViewBag.Cliente = client.First().Name;

        return View();
    }
}

请务必使用此文档作为最佳做法的参考:https://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-dotnet-how-to-use-client-library/