如何在ASP.NET MVC应用程序中异步查询ParseObject集合?

时间:2018-04-26 00:20:34

标签: c# asp.net asp.net-mvc parse-platform parseobject

我尝试了以下内容。目前,我只是想读取控制台中查询的对象集合。根据该指南,首选异步查询:http://docs.parseplatform.org/dotnet/guide/#queries

public class IndexModel : PageModel
    {
        public void OnGet()
        {
            Task<int> task = HandleMessagesAsync();
            task.Start();

        }

        private async Task<int> HandleMessagesAsync()
        {
            var query = ParseObject.GetQuery("Message");
            IEnumerable<ParseObject> results = await query.FindAsync();

            Console.WriteLine(results);

            throw new NotImplementedException();
        }
    }

我收到以下错误:

System.InvalidOperationException: 'Start may not be called on a promise-style task.'

1 个答案:

答案 0 :(得分:0)

Task课程会根据本指南自动启动:https://social.msdn.microsoft.com/Forums/vstudio/en-US/70f82b79-188e-4e91-8f86-b5a9382663fb/problem-with-taskstart?forum=netfxbcl

无需调用.Start();

public class IndexModel : PageModel
    {
        public void OnGet()
        {
            Task<int> task = HandleMessagesAsync();
        }

        private async Task<int> HandleMessagesAsync()
        {
            var query = ParseObject.GetQuery("Message");
            IEnumerable<ParseObject> results = await query.FindAsync();
            var count = await query.CountAsync();

            Debug.WriteLine("# Message: " + count);

            throw new NotImplementedException();
        }
    }

我使用Debug.WriteLine("# Message: " + count);来确保从我的数据库中查询正确数量的Message对象。