AggregateException when attempting Rest API calls

When attempting to call on Youtrack Rest API calls I keep encountering a System.AggregateException. I've tested the application on three computers and the error is only encountered on one of the three computers. I've been unable to find a solution online that resolves the issue or the exact cause of the issue and why only 1 out of 3 computers is affected by the issue. 

Test Code to run Youtrack Rest API:

using System;
using System.IO;
using YouTrackSharp;

namespace ConnectionTest
{
class Program
{

static void Main(string[] args)
{
Console.WriteLine("Running Youtrack Tests...");

RunConnectionTest();
RunIssueTest();
RunIssueCountTest();

Console.WriteLine("Test Complete, press any key to exit.");
Console.ReadKey();
}

static void RunConnectionTest()
{

try
{
var connection = new UsernamePasswordConnection("https://cgicfuskara.myjetbrains.com/youtrack", "alexander.kelman", "redacted");
var conTest = connection.GetAuthenticatedHttpClient();
conTest.Wait();
if(conTest.IsCompleted)
{
Write("Connection Complete");
}
else
{
Write("Connection NOT complete");
}

Console.WriteLine("Connection Test - Complete");
}
catch(Exception e)
{
Write("Unable to connect");
Write(e.ToString());

Console.WriteLine("Connection Test - Failed");
}
}

static void RunIssueTest()
{
try
{
var connection = new UsernamePasswordConnection("https://cgicfuskara.myjetbrains.com/youtrack", "alexander.kelman", "redacted");
var issues = connection.CreateIssuesService().GetIssues("resolved date: {Last week} .. Today ", take: 1000);

issues.Wait();

if(issues.IsCompleted)
{
Write("Issue Retrieval Complete");
}
else
{
Write("Issue Retrieval NOT Complete");
}

Console.WriteLine("Issue Retrieval Test - Complete");
}
catch(Exception e)
{
Write("Unable to retieve issues");
Write(e.ToString());
Console.WriteLine("Issue Retrieval Test - Failed");
}
}

static void RunIssueCountTest()
{
try
{
var connection = new UsernamePasswordConnection("https://cgicfuskara.myjetbrains.com/youtrack", "alexander.kelman", "redacted");
var issues = connection.CreateIssuesService().GetIssueCount("resolved date: {Last week} .. Today ");

issues.Wait();

if (issues.IsCompleted)
{
Write("Issue Count Complete");
}
else
{
Write("Issue Count NOT Complete");
}
Console.WriteLine("Issue Count Test - Complete");
}
catch(Exception e)
{
Write("Unable to count issues");
Write(e.ToString());
Console.WriteLine("Issue Count Test - Failed");
}
}

static void Write(string text)
{
using (StreamWriter writer = new StreamWriter("Log.txt", true))
{
writer.WriteLine(text);
}
}
}
}

 

 

Stack Trace:
Unable to connect
System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection. The connection was closed before all data could be read. Expected 4099 bytes, read 0 bytes.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.StreamToStreamCopy.<CopyAsyncToPreSizedLimitMemoryStream>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.UsernamePasswordConnection.<GetAuthenticatedHttpClient>d__5.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at ConnectionTest.Program.RunConnectionTest() in C:\Users\alexander.kelman\source\repos\CGI Tools\ConnectionTest\Program.cs:line 32
---> (Inner Exception #0) System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection. The connection was closed before all data could be read. Expected 4099 bytes, read 0 bytes.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.StreamToStreamCopy.<CopyAsyncToPreSizedLimitMemoryStream>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.UsernamePasswordConnection.<GetAuthenticatedHttpClient>d__5.MoveNext()<---

Unable to retieve issues
System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection. The connection was closed before all data could be read. Expected 4099 bytes, read 0 bytes.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.StreamToStreamCopy.<CopyAsyncToPreSizedLimitMemoryStream>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.UsernamePasswordConnection.<GetAuthenticatedHttpClient>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.Issues.IssuesService.<GetIssues>d__19.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at ConnectionTest.Program.RunIssueTest() in C:\Users\alexander.kelman\source\repos\Tools\ConnectionTest\Program.cs:line 59
---> (Inner Exception #0) System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection. The connection was closed before all data could be read. Expected 4099 bytes, read 0 bytes.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.StreamToStreamCopy.<CopyAsyncToPreSizedLimitMemoryStream>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.UsernamePasswordConnection.<GetAuthenticatedHttpClient>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.Issues.IssuesService.<GetIssues>d__19.MoveNext()<---

Unable to count issues
System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection. The connection was closed before all data could be read. Expected 4099 bytes, read 0 bytes.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.StreamToStreamCopy.<CopyAsyncToPreSizedLimitMemoryStream>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.UsernamePasswordConnection.<GetAuthenticatedHttpClient>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.Issues.IssuesService.<GetIssueCount>d__20.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at ConnectionTest.Program.RunIssueCountTest() in C:\Users\alexander.kelman\source\repos\Tools\ConnectionTest\Program.cs:line 87
---> (Inner Exception #0) System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection. The connection was closed before all data could be read. Expected 4099 bytes, read 0 bytes.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.StreamToStreamCopy.<CopyAsyncToPreSizedLimitMemoryStream>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.UsernamePasswordConnection.<GetAuthenticatedHttpClient>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at YouTrackSharp.Issues.IssuesService.<GetIssueCount>d__20.MoveNext()<---


0
5 comments
Avatar
Maarten Balliauw

First of all, please reset your YouTrack password. I have updated the code above to remove your password, but it has been public for a while so best to take precautions.

That said, the error seems to originate from Microsoft's HttpClient class. Can you try the following code on the computer that throws the error?

var httpClient = new HttpClient();
var result = await httpClient.GetStringAsync("https://cgicfuskara.myjetbrains.com/youtrack");

This should, in theory, throw the same error and may mean there is a connectivity issue between that particular PC and https://cgicfuskara.myjetbrains.com/youtrack

0
Avatar
Permanently deleted user

Completely forgot that I hardcoded the password, thank you for fixing that! 

You were correct it was a HttpClient error, as the same exception was thrown. Seeming as the computers are on the same net is the issue just due to different firewall/open ports then possibly?

0
Avatar
Maarten Balliauw

Could be, really depends on PC configuration, firewalls, proxy, ...

0
Avatar
Permanently deleted user

Well now that I know the problem is a computer configuration and not code based I can start looking at the right place, thank you very much for your help!

0
Avatar
Maarten Balliauw

You're welcome!

0

Please sign in to leave a comment.