.NET HTTPClient Asynchronous Limitations
I have a small .Net 4.5 C# app which reads information from a data source
and then pushes this information to a web site which is a .NET 4.5 Web API
site with a simple controller. The controller receives the data and puts
it into a database.
The following works for me, as fast as the application can read it can
write and everything ends up in the DB:
public static void PostDataToWebApi(MyDataClass tData)
{
HttpResponseMessage s = null;
try
{
s = client.PostAsJsonAsync("/api/Station/Collector",
tData).Result;
s.EnsureSuccessStatusCode();
}
catch (Exception e)
{
Console.WriteLine("ERROR (ClientPost): " + e.ToString());
}
}
The following does NOT work. It POSTs about a thousand-odd records and
then comes up with a number of errors all with the message "a task was
canceled":
public static async void PostDataToWebApi(MyDataClass tData)
{
HttpResponseMessage s = null;
try
{
s = client.PostAsJsonAsync("/api/Station/Collector", tData);
s.EnsureSuccessStatusCode();
}
catch (Exception e)
{
Console.WriteLine("ERROR (ClientPost): " + e.ToString());
}
}
The full error is:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)
at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at
IICE_DataCollector_Remote.Program.<PostStationLocationToWebApi>d__7.MoveNext()
in e:\Users\TestUser.TEST\Documents\Visual Studio
2012\Projects\Test_App-trunk\TestCollector\Program.cs:line 475
Any quick fixes for this? From what I can tell it runs out of something,
threads, sockets, who knows :-)
Any pointers would be appreciated, I'd love to get this working, as you
can imagine doing the POST synchronously is considerably slower than
asynchronously.
No comments:
Post a Comment