难道Web服务的异步调用使服务器有意义吗? [英] Does async calling of web service make sense on server?

查看:190
本文介绍了难道Web服务的异步调用使服务器有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web服务器(ASP.NET MVC 4)我呼吁另一台服务器上的Web服务(ASMX)。我已经生成的客户端从WSDL。有同步方式和异步方法(回调,而不是任务)。

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).

我将其转换为面向任务的调用(使用 TaskCompletionSource ),然后我叫等待是这样的:

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;
}

在我使用异步 / 等待随处可见。这是否有什么意义?我认为,当Web服务被称为我救一个线程与此形式给出 - 是吗?难道Web服务客户端阻塞线程时异步加载?如果是这样我可以使用同步methoad来代替:

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();
}

感谢

BTW:我使用的ASP.NET MVC 4,针对.NET 4(因为我要)。对于异步 / 等待兼容性我使用Microsoft.Bcl.Async库。

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.

推荐答案

您的权利,使用异步IO也挽救一个线程在运行。这是在服务器上异步IO的主要优势。 我已经写的优点和异步IO之前的利弊(另外:<一href="http://stackoverflow.com/a/12796711/122718">http://stackoverflow.com/a/12796711/122718).

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: http://stackoverflow.com/a/12796711/122718).

你应该怎么做?如果网络服务往往响应缓慢或具有有时候响应的风险慢慢异步是相当有吸引力的。图片10请求进来每秒具有Web服务10秒的等待时间(可能是由于性能问题)。然后,你需要100个线程只服务的负荷。

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.

在另一方面,如果你不惧怕任何这一点,异步会给你什么。您节省几MB,你可能不需要堆栈内存。您也消耗更多的CPU采用异步。你成为生产力较低(尤其是因为你现在需要做这种方法的所有调用异步为好。异步是病毒)。

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.).

这篇关于难道Web服务的异步调用使服务器有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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