创建异步网络服务方法 [英] Creating an async webservice method

查看:17
本文介绍了创建异步网络服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试阅读异步方法,现在正在尝试创建自己的异步方法.该方法是一个返回错误日志列表的网络服务调用.我不确定我是否理解正确,所以我想我会分享我的代码,看看我是否应该做一些不同的事情.

I've tried to read up on async methods and am now trying to create my own async method. The method is a webservice call that returns a list of error logs. I'm not sure that I've understood correctly so I thought I'd share my code to see if I should do anything different.

我想让代码做的就是通过调用方法 GetAllErrorLogs() 返回错误日志列表,这是一个同步方法.由于获取所有错误日志可能需要一秒钟时间,因此我希望在调用 GetAllErrorLogs() 方法后有机会做其他事情.这是代码.

All I want the code to do is return a list of errorlogs by calling a method GetAllErrorLogs(), that is a synchronized method. Since it can take a second to fetch all the error logs I want to have the opportunity to do other stuff once I called the GetAllErrorLogs() method. Here is the code.

[WebMethod]
public async Task<List<ErrorLog>> GetAllErrorLogs()
{
    List<ErrorLog> errorLogs = new List<ErrorLog>();

    await System.Threading.Tasks.Task.Run(() => {
        errorLogs = ErrorLogRepository.GetAllErrorLogs();
    });


    if (errorLogs == null)
        return new List<ErrorLog>();

    return errorLogs;
}

谢谢!

推荐答案

我最近在 ThatConference 发表了演讲在服务器端的 async,我在幻灯片中解决了这个问题.

I recently gave a talk at ThatConference on async on the server side, and I address this issue in the slides.

在服务器端,您希望避免使用 Task.Run 和其他将工作排队到线程池的构造.尽可能保持线程池线程可用于处理请求.

On the server side, you want to avoid the use of Task.Run and other constructs that queue work to the thread pool. As much as possible, keep thread pool threads available for handling requests.

因此,理想情况下,您的存储库应该有一个异步方法 GetAllErrorLogsAsync,它本身就是异步的.如果GetAllErrorLogs不能异步,那你也可以直接调用(去掉await Task.Run).

So, ideally your repository would have an asynchronous method GetAllErrorLogsAsync, which would itself be asynchronous. If GetAllErrorLogs cannot be asynchronous, then you may as well just call it directly (removing the await Task.Run).

由于获取所有错误日志可能需要一秒钟的时间,因此我希望在调用 GetAllErrorLogs() 方法后有机会做其他事情.

Since it can take a second to fetch all the error logs I want to have the opportunity to do other stuff once I called the GetAllErrorLogs() method.

如果您有可用的 GetAllErrorLogsAsync,则可以使用 Task.WhenAll 轻松完成.然而,如果 GetAllErrorLogs 是同步的,那么你只能通过在你的请求中做并行工作来做到这一点(例如,多次调用 Task.Run 后跟 Task.当全部).

If you have a GetAllErrorLogsAsync available, then this can easily be done using Task.WhenAll. However, if GetAllErrorLogs is synchronous, then you can only do this by doing parallel work in your request (e.g., multiple calls to Task.Run followed by Task.WhenAll).

服务器上的并行代码必须非常谨慎.它只在非常有限的一组场景中是可以接受的.在服务器端async 的全部意义在于为每个请求使用更少 个线程,当你开始并行化时,你正在做相反的事情:multipleem> 每个请求的线程数.这仅适用于您知道您的用户群非常小的情况;否则,您将破坏服务器的可扩展性.

Parallel code on the server must be approached with great trepidation. It is only acceptable in a very limited set of scenarios. The entire point of async on the server side is to use fewer threads per request, and when you start parallelizing, you're doing the opposite: multiple threads per request. This is only appropriate if you know your user base is very small; otherwise, you'll kill your server scalability.

这篇关于创建异步网络服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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