Does async calling of web service make sense on server?

房东的猫 提交于 2019-12-11 02:24:26

问题


On my web server (ASP.NET MVC 4) I am calling web services (asmx) on another server. I have generated client from WSDL. There are sync methods and async methods (callbacks, not Tasks).

I transform it to task oriented call (with TaskCompletionSource) and then i call await like this:

public async Task<DataObject> GetData()
{
    var tcs = new TaskCompletionSource<DataObject>();

    var client = new WebServiceClient();
    client.GetDataCompleted += (sender, args) => tcs.SetResult(args.Result);
    client.GetDataAsync();

    return await tcs.Task;
}

After that I use async/await everywhere. Does this make any sense? I think that when the web service is being called I save one thread with this aproach - am I right? Does the web service client block a thread when async loading? If so I could use sync methoad instead:

public DataObject GetData()
{
    var client = new WebServiceClient();
    return client.GetData();
}

Thanks

BTW: I am using ASP.NET MVC 4, targeting .NET 4 (because I have to). For async/await compatibility I am using Microsoft.Bcl.Async library.


回答1:


You are right in that using async IO does "save" one thread while it is running. This is the main advantage of async IO on the server. I have written about the pros and cons of async IO before. (Also: https://stackoverflow.com/a/12796711/122718).

What should you do? If that web-service tends to respond slowly or has a risk of sometimes responding slowly async is quite attractive. Image 10 requests coming in per second and the web service having 10s latency (maybe due to a performance problem). Then, you need 100 threads just to serve that load.

On the other hand, if you don't fear any of this, async will give you nothing. You save a few MB of stack memory which you likely don't need. You also burn more CPU with async. You become less productive (especially because you now need to make all callers of this method async as well. Async is viral.).



来源:https://stackoverflow.com/questions/25288390/does-async-calling-of-web-service-make-sense-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!