c# - .Net Core 中的异步委托
问题描述
我正在尝试使用异步调用和委托来改进 Azure 函数 API 的性能。我现在得到了预期的结果,它只需要 2 秒就可以给我回复。但是在幕后,当我查看应用程序洞察力时,我观察到一些繁重的过程正在进行,这是我之前在实施委托之前进行 Jmeter 负载测试时没有遇到的。
public async Task Mainmethod()
{
Test1 test1=new Test1();
test1.mymethod1();//trigger method. Don't wait for the output ,execute the next line and return
response
console.writeline("Request submitted successfully");
}
public class Test1{
public Task Delgate MyDelegate();
public async Task mymethod1(){
MyDelegate myDelegate=mymethod2;
myDelegate=new myDelegate(mymethod2);
Task.Run(() =>myDelegate.Invoke());//.Net core we dont have method for begininvoke. So, placed inside a
task to execute
}
public async Task mymethod2(){
string x=await GetAccessToken();//external call for get token (1 request --- 1 sec ; 10 request 1 sec)
string y= await DoWork(x);//external call for get info (1 request --- 2 sec ; 10 request 14sec)
2
Dosomemorework(y);//external call for make transaction (1 request --- 3 sec ; 10 request 26 sec)
SendEmail();//send async email
}
public Task async GetAccessToken(){
//await call for get accessToken from service provider
}
public Task async DoWork(string x){
//await call for get response from service provider
}
public Task async Dosomemorework(string y){
//await call for post information from service provider
}
public Task async SendEmail(){
await smtp.SendMailAsync(mailMessage);
}
}
My problem is, infuture when there is more load on the API. The time may increase,and possibility of timeouts.I sense some queuing mechanism is happening , or running the code on a single thread.How to overcome this.
解决方案
推荐阅读
- reactjs - 提交时 React-select defaultValue 未定义
- python - 如何用 selenium 和 python 填充 javascript 文本框(没有 send_keys)
- sas - 使用直通执行 SAS 代码时出错
- c# - 解析枚举的完全限定字符串表示,包括命名空间/类型
- javascript - 将自定义声明与 Firestore 通配符匹配时出现 onSnapshot 权限错误
- r - Unnest Google Analytics 嵌套数据 DPLYR
- microsoft-graph-api - Graph API 错误将重复从“noEnd”更改为“numbered”
- c - 与子线程共享存储在主线程堆栈中的互斥锁
- spring-boot - Spring boot 2.2.x 应用程序是否可部署到 Servlet 3.0 容器
- python - 这段代码如何只打印出字符串的首字母?