创建异步Web服务方法 [英] Creating an async webservice method
问题描述
我试着读上异步方法,我现在想创建自己的异步方法。该方法是一个Web服务调用返回的错误日志列表。我不知道,我已经正确地理解,所以我想我会分享我的code,看看我应该做的有什么不同。
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.
我想要的code做的是通过调用一个方法的 GetAllErrorLogs()的,这是一个同步的方法返回errorlogs的列表。因为它可以花一秒钟来获取所有我希望有机会做其他的东西,错误日志,一旦我叫的 GetAllErrorLogs()的方法。这里是code。
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 的<一个href="http://blog.stephencleary.com/2013/08/slides-available-thatconference-2013.html"><$c$c>async$c$c>在服务器端和我在幻灯片中解决这个问题。
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
不能是异步的,那么你可能也只是直接调用它(删除等待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.WhenAll
)。
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
).
在服务器上并行code必须是接近十分惶恐。它只有在一组场景非常有限的可以接受的。对服务器端的整点异步
是使用的少的每个请求的线程,而当你开始并行化,你正在做相反的: 多的每个请求的线程。这仅仅是适当的,如果你知道你的用户群是非常小的;否则,你会杀了你的服务器的可扩展性。
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.
这篇关于创建异步Web服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!