c# - .NET HTTPClient Asynchronous Limitations -


i have small .net 4.5 c# app reads information data source , pushes information web site .net 4.5 web api site simple controller. controller receives data , puts database.

the following works me, fast application can read can write , ends in 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 not work. posts thousand-odd records , comes number of errors message "a task canceled", after 10 seconds 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:

    @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)     @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)     @ system.runtime.compilerservices.taskawaiter`1.getresult()     @ 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 this? can tell runs out of something, threads, sockets, knows :-)

any pointers appreciated, i'd love working, can imagine doing post synchronously considerably slower asynchronously.

just sure wasn't machine, local anti-virus or network have tried on w2k8 r2 server, windows 7 virtual guest desktop (fresh build) , windows 8 machine well, same result.

more info : have tested partial success lan connection smaller data set (10,000 records), , defaultconnectionlimit of 100. but, in production 500,000 records, when posting remote server across internet (still low latency 25ms-50ms) have not had success.

thanks in advance :-)

ok, have working now. biggest thing fine-tune settings on client end, server. these settings different depending on whether running test locally or on internet.

my "postdatatowebapi" method looks this:

    public static async void postdatatowebapi(mydataclass tdata)         {          await throttler.waitasync();         alltasks.add(task.run(async () =>         {              try             {                 var s = await client.postasjsonasync("/api/station/collector", tdata).configureawait(false);             }             catch (exception e)             {                 console.writeline("error (clientpost): " + e.tostring());             }                         {                 throttler.release();             }         }));     } 

i have following declared @ top of console application:

    private static list<task> alltasks = new list<task>();     private static semaphoreslim throttler; 

before loop starts have following, variables changed make sure works:

    servicepointmanager.defaultconnectionlimit = _defaultconnections;     servicepointmanager.maxservicepointidletime = _maxidletime;     servicepointmanager.expect100continue = false;     servicepointmanager.checkcertificaterevocationlist = false;      throttle = new semaphoreslim(initialcount: _maxqueue); 

as guide, internet based transaction following works me:

  1. default connections : 24
  2. max idle time : 400
  3. semaphoreslim initial count: 50

for lan test run both default connections , initial count values higher without problem, expected guess :-)

finally, outside have following make sure don't kill tasks still running @ end of execution run:

     await task.whenall(alltasks); 

hope helps!


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -