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", but then after about 10 seconds it resumes processing:
public static async void PostDataToWebApi(MyDataClass tData)
{
HttpResponseMessage s = null;
try
{
s = await 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.<PostDataToWebApi>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.
Just to be sure it wasn't my machine, local anti-virus or network I have tried on a W2k8 R2 server, a Windows 7 virtual guest desktop (fresh build) and a Windows 8 machine as well, with the same result.
More Info : I have tested this with partial success from a LAN connection with a smaller data set (10,000 records), and a DefaultConnectionLimit of 100. But, in production with 500,000 records, when posting to a remote server across the Internet (still low latency 25ms-50ms) I have not had any success.
Thanks in advance for any help :-)