问题描述:

How can I make a synchronous call to a Mobile Service Table from MVC?

When I run the following code, My application keeps hanging forever waiting the result:

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();

}

}

Funny, the same code in my Unit Test works perfectly.

How can I list my Mobile Service Table clients Synchronously?

网友答案:

It looks like the problem you're having is because you're not using await with the async methods.

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

Be sure to use this doc as a reference on best practices: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-dotnet-how-to-use-client-library/

相关阅读:
Top