(Solved) IHttpClientFactory problem with requests
i have Task:
private async System.Threading.Tasks.Task<Discord.FileAttachment> XTAtachmetToFileAttachment(Discord.IAttachment xAAttachment)
{
Discord.FileAttachment xFileAttachmentToReturn; // download file to Stream System.Net.Http.IHttpClientFactory xOHttpClientFactory = Bot_One.XCDependContainer.XOServiceProvider.GetService<System.Net.Http.IHttpClientFactory>(); System.Net.Http.HttpClient xHttpClient = xOHttpClientFactory.CreateClient(); System.IO.Stream xStream = await xHttpClient.GetStreamAsync(xAAttachment.Url); and so on... when i run that task concurrent (5 requests), 2 request are completed and other 3 not... and run i like this (with list): System.Collections.Generic.List<System.Threading.Tasks.Task<Discord.FileAttachment>> xTasksToExecute = new System.Collections.Generic.List<System.Threading.Tasks.Task<Discord.FileAttachment>>(); foreach (Discord.IAttachment xOCollectedAttachment in xOMessageForPosting.XEnCollectedAttachments) { xTasksToExecute.Add(System.Threading.Tasks.Task.Run( () => XTAtachmetToFileAttachment(xOCollectedAttachment) } System.Console.WriteLine($"Befor caling WhenAll."); var result = await System.Threading.Tasks.Task.WhenAll(xTasksToExecute); and i ran out of ideas what could be wrong...
Discord.FileAttachment xFileAttachmentToReturn; // download file to Stream System.Net.Http.IHttpClientFactory xOHttpClientFactory = Bot_One.XCDependContainer.XOServiceProvider.GetService<System.Net.Http.IHttpClientFactory>(); System.Net.Http.HttpClient xHttpClient = xOHttpClientFactory.CreateClient(); System.IO.Stream xStream = await xHttpClient.GetStreamAsync(xAAttachment.Url); and so on... when i run that task concurrent (5 requests), 2 request are completed and other 3 not... and run i like this (with list): System.Collections.Generic.List<System.Threading.Tasks.Task<Discord.FileAttachment>> xTasksToExecute = new System.Collections.Generic.List<System.Threading.Tasks.Task<Discord.FileAttachment>>(); foreach (Discord.IAttachment xOCollectedAttachment in xOMessageForPosting.XEnCollectedAttachments) { xTasksToExecute.Add(System.Threading.Tasks.Task.Run( () => XTAtachmetToFileAttachment(xOCollectedAttachment) } System.Console.WriteLine($"Befor caling WhenAll."); var result = await System.Threading.Tasks.Task.WhenAll(xTasksToExecute); and i ran out of ideas what could be wrong...
6 Replies
What do you mean with "not completed"? Not yet completed? Thorwing errors?
well... i addted console line at the Task beginning and end.. and i get like this
Befor caling WhenAll.
XTAtachmetToFileAttachment Task running.
XTAtachmetToFileAttachment Task running.
XTAtachmetToFileAttachment Task running.
XTAtachmetToFileAttachment Task running.
XTAtachmetToFileAttachment Task running.
XTAtachmetToFileAttachment Task completed.
XTAtachmetToFileAttachment Task completed.
after i get errors huge stack.. something like:
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()
at Bot_One.XNUtilites.XCSendMsessages.<XTAtachmetToFileAttachment>d__1.MoveNext() in D:\CModules\Bot One\Bot One\XNUtilites\XCSendMsessages.cs:line 161
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()
at Bot_One.XNUtilites.XCSendMsessages.<XTAtachmetToFileAttachment>d__1.MoveNext() in D:\CModules\Bot One\Bot One\XNUtilites\XCSendMsessages.cs:line 161Is this stack trace part of the code you did show earlier?
That's console output...
5 tast are fired from:
var result = await System.Threading.Tasks.Task.WhenAll(xTasksToExecute);
and 2 of them completed...
URL's i think good... if i use Task XTAtachmetToFileAttachment(xOCollectedAttachment) in foreach loop... all is OK. But when i try parallel... its not completing...
So because you are using HttpClientFactory, it might be that you did set a default timeout which causes a cancellation. But it's honestly pretty much impossible to diagnose this issue without more details. When you say the requests do work in sequence, but not concurrently, that might indicate the server is not capably of responding fast enough with a few concurrent requests
well... for beginning there was not setting timeout... so it was by default 100 seconds... later i set it to 20 seconds... that i dont need to wait too long for error 🙂
And i'm query Discord... so i think it can handle 5 instant requests... all so i tried from other servers... result same... 2 pass.. other hangs...
i think i solved this... i will rearage code and post what i did...