首页 > 解决方案 > .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.


标签: c#.net-coreasync-awaitdelegatesazure-functions

解决方案


推荐阅读