TPL用于服务呼叫 [英] TPL used for Service Call

查看:117
本文介绍了TPL用于服务呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个来自一个方法的5个WEB API调用 - 之前它是同步调用并且需要花费一些时间10秒。现在我想实现TASK并使用以下方式调用所有服务:


Task.Factory.StartNew(); 


并收集结果使用 


TaskDomainDependentList.GetAwaiter()。GetResult();


在我的方法中需要它时,但mnethod仍需要相同的时间。  ;


您能否发送一些链接,其中显示了在API API调用中最佳使用TPL的效果。



谢谢


Santosh Jha


解决方案

如果你想让你的webApi"动态调用" (我的意思是你可以将你的WebApi地址列入存储在项目中的一张纸上,然后将它们读出来)。
Parallel.Invoke  也是您的选择。


也许您使用不同类型的WebApi地址进行相同的操作或处理......,您可以使用HttpClient来处理它(将其设为调用它的功能)


示例代码:

 

行动[] actions = new Action [Addresses.Count];

int index = 0;

foreach(地址中的字符串地址)//"地址"是List< string>来自某些地方,例如配置文件......

{

string localKeep = address;

actions [index ++] = new Action(()=> ; {//使用带有localKeep的HttpClient作为地址});

}

Parallel.Invoke(动作);


Hi All,

I have 5 WEB API call from one method - earlier it was call Synchronously and was taking some time 10 sec. Now I thought to implement TASK and call all the service using :

Task.Factory.StartNew(); 

and harvested the result by using 

TaskDomainDependentList.GetAwaiter().GetResult();

when its needed in my method, but still the mnethod takes same time. 

can you please send some link which shows best use of TPL for performance in WEB API calling.

Thanks

Santosh Jha

解决方案

If you want to make your webApi "dynamically called" (I mean may be you can list your WebApi addresses into a piece of paper stored into your project, and then read them out). Parallel.Invoke is also your choice.

Maybe you have the same actions or processes with a differnt kind of WebApi Address……, you can use HttpClient to cope with that (Make it as a function to call it)

Sample code:

Action[] actions = new Action[Addresses.Count];

int index=0;

foreach (string address in Addresses) //"Addresses" is a List<string> coming from some place such as config files...

{

string localKeep = address;

actions[index++] = new Action(()=>{ //Use your HttpClient with localKeep as address });

}

Parallel.Invoke(actions);


这篇关于TPL用于服务呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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