HTTPCLIENT.POSTASYNC()。Result获取错误System.Threading.Tasks.TaskCanceledException:任务已取消

时间:2019-06-30 10:10:11

标签: multithreading asp.net-web-api

我每天晚上7点使用Windows服务快速发送10,000封电子邮件。我使用Web API发送电子邮件。

I am using Task=>Run() method calling Web API to send emails.

**Below is code snapshot**



private static readonly HttpClient Client = new HttpClient();

    Void SendemailByAPI(){
      Task.Run(() => SendemailByAPI());
      }

   public SendemailByAPI()
    {    
     try{

    HttpResponseMessage response = client.PostAsync(apiName,httpContent).Result;

      return response
    }
      catch(exception ex)
    {
      writelog.log(ex.tostring());

    }
    }

通过此代码,某些电子邮件正在发送,而某些电子邮件不是通过WEB API电子邮件发送的     在我的自定义日志文件中记录的以下错误消息是-任务已取消。

LLogMessage:System.AggregateException:发生一个或多个错误。 ---> System.Threading.Tasks.TaskCanceledException:任务被取消。    ---内部异常堆栈跟踪的结尾---    在System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)    在System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 1.get_Result()    在D:\ VSTS \ Reference \ Development \ Code \ SkillSurvey.Common \ EmailAPIGateway \ ApiGateway.cs:line 91中的SkillSurvey.Common.ApiGateway.PostAsyncRequest(HttpClient client,String baseAddressURL,String apiName,String parameterJsonString) --->(内部异常#0)System.Threading.Tasks.TaskCanceledException:任务已取消。

**Error from Windows HTTP.SYS errors are logged to %windir%\System32\LogFiles\HTTPERR** 

-10 Request cancelled apppool name
-10 Request cancelled apppool name ---etc

**I am not getting why this eroor occurs** 
1.Task=>Run() OR  2. HTTPCLIENT timeout OR 3. httpclient.PostAsyc().Result 4.WEBAPI app pool size (queue length 1000)

Threading issue or httpclient object timeout or calling many webapi requests at a time

I was tried to solve this issue below things but not resolved it Plz help me.

1. remove Task=>Run() and call directly WEB API URL.
2. Remove .Result and only call httpclient.PostAsyc(string url,content)--task cancel not error logged in a custom file but not send all emails but in HTTP.SYS same error -10 Request cancelled the app pool name

Can anyone get the same problem? Thanks in advance for your any suggestion. 

0 个答案:

没有答案