【问题标题】:return object task on web service在 Web 服务上返回对象任务
【发布时间】:2014-10-23 08:45:07
【问题描述】:

我正在尝试调用应该运行异步任务的 Web 服务。这个异步任务应该遍历记录数组并处理它们。每条要处理的记录都会生成一个结果。我想要的是在列表中连接这些结果。当调用 Web 服务时,我想从调用者的 HttpResponse 中检索这样的列表,但我不知道如何去做。

调用函数的代码是:

private void ProcessRecords(List<Record> recordList)
    {
                //The WS is called and a response is awaited
                HttpResponseMessage response = client.PostAsJsonAsync("/api/mycontroller/myws/", recordList).Result;                    
                //TODO: Read the result list from the http response
    }

我的Web Service的代码如下:

    [HttpPost]
    [Route("myws")]
    public async Task<IHttpActionResult> WebServiceMethod()
    {
        var jsonString = await Request.Content.ReadAsStringAsync();
        List<Record> recordList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Record>>(jsonString); 
        Task<Result> resultTask = CreateTaskToProcessRecords(recordList);

        //TODO I Do not now what to return here in order 
        //for it to contain the resultTask variable and later await it in the user function
    }

private Task<List<Result>> CreateTaskToProcessRecords(List<Record> recordList)
    {   
        var newTask = Task.Run<List<Result>>(() =>
        {
            List<Result> resultList = new List<Result>();
            try
            {
                foreach(Record record in recordList)
                {
                 var result = DoSomething(record);
                 resultList.Add(result);
                }
                return resultList;
            }
            catch (Exception ex)
            {
                return resultList;
            }
        });
        return newTask;
    }

我想要做的是以某种方式将 Task> 返回给调用 Web 服务的函数,以便在 Web 服务“newTask”中完成的整个处理过程保持异步。 你有什么想法? 谢谢 路易斯。

【问题讨论】:

  • 不要返回这样的Task,异步调用即可。

标签: asp.net-mvc web-services asynchronous asp.net-web-api


【解决方案1】:

您的所有工作都是同步的。这里不需要asyncawait,所以不要使用它们。 (作为一般规则,请避免在 ASP.NET 上使用 Task.Run)。

[HttpPost]
[Route("myws")]
public IHttpActionResult WebServiceMethod()
{
    var jsonString = await Request.Content.ReadAsStringAsync();
    List<Record> recordList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Record>>(jsonString);
    var results = ProcessRecords(recordList);
    return Json(results);
}

private List<Result> ProcessRecords(List<Record> recordList)
{   
    List<Result> resultList = new List<Result>();
    try
    {
        foreach(Record record in recordList)
        {
             var result = DoSomething(record);
             resultList.Add(result);
        }
        return resultList;
    }
    catch (Exception ex)
    {
        return resultList;
    }
}

请注意,您仍然可以在客户端异步使用它:

private async Task ProcessRecordsAsync(List<Record> recordList)
{
    // The WS is called and a response is awaited
    HttpResponseMessage response = await client.PostAsJsonAsync("/api/mycontroller/myws/", recordList);
    var result = await response.Content.ReadAsAsync<List<Result>>();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    相关资源
    最近更新 更多