c# - WCF REST Not Processing Asynchronously -


we implementing new wcf rest service in iis our site , on number of pages may making handful of ajax calls using jquery asynchronously. problem seems though wcf (on server side) executing synchronously.

on page load we're making 3 separate calls 3 different methods. using logging, can see them hit global.asax file within 5ms of each other. there, logging shows executing in order exit global.asax (not order in made calls page via javascript). expected each call receive own thread , return individually. when attaching debugger can see won't execute next method until step through current method it's on.

here operation contracts 3 of methods 'thought' implemented use async model.

    [operationcontract(asyncpattern = true)]     [webinvoke(         method = "post"          , uritemplate = "/listuserpreferences"         , bodystyle = webmessagebodystyle.wrapped         , responseformat = webmessageformat.json         , requestformat = webmessageformat.json     )]     iasyncresult beginlistuserpreferences(asynccallback callback, object state);     result<list<data.enumerationitem<userpreferencetype>>> endlistuserpreferences(iasyncresult asyncresult);      [operationcontract(name = "getusersecure", asyncpattern = true)]     [webinvoke(         method = "post"          , uritemplate = "/getuser"         , bodystyle = webmessagebodystyle.wrapped         , responseformat = webmessageformat.json         , requestformat = webmessageformat.json     )]     iasyncresult begingetuser(asynccallback callback, object state);     result<data.user> endgetuser(iasyncresult asyncresult);      [operationcontract(asyncpattern = true)]     [webinvoke(         method = "post"          , uritemplate = "/listwithattributes"         , bodystyle = webmessagebodystyle.wrapped         , responseformat = webmessageformat.json         , requestformat = webmessageformat.json     )]     iasyncresult beginlistwithattributes(int index, int pagesize, asynccallback callback, object state);     result<pagedcollection<data.attribute>> endlistwithattributes(iasyncresult asyncresult); 

here example of 1 of implementations in service.

    public iasyncresult begingetuser(asynccallback callback, object state)     {         var asyncresult = new completedasyncresult<result<data.user>>(state);         asyncresult.result = new result<data.user>();          asyncresult.result.value.userid = guid.empty;         asyncresult.result.value.displayname = "asdfasd";         asyncresult.iscompleted = true;                     callback(asyncresult);          return asyncresult;     }      public result<data.user> endgetuser(iasyncresult asyncresult)     {         return ((completedasyncresult<result<data.user>>)asyncresult).result;     } 

here attributes have on service implementation class.

[aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)] [servicebehavior(instancecontextmode = instancecontextmode.percall, concurrencymode = concurrencymode.multiple)] 

can please provide insight why these executing synchronously , need do, or @ least point me in direction of need do, these execute asynchronously?

update

i took of matt's answer , moved logic end function calls , followed blog post on how did bit more closely uploading large files self hosted wcf rest service. however, couldn't end methods call using technique. i'm starting think i'm going wrong way. because looking @ logs, custom authentication service being executed before each service call, happens before async method operations fire. after further investigation, took @ threadid's of each request coming iis , executing operations. appears wcf using 1 worker thread... period. doesn't matter how many requests send @ time iis. example, if send 3 requests, can see come in @ different times (within milliseconds of each other) , own thread. seems wcf queues them , executes them in order because they're executed on same thread, including authentication service calls.

it looks me, example, you're doing work before "begin" call finishes; that's common mistake found while learning async pattern.

though i'm not familiar application in wcf, async model more typically begin method launches new thread iasyncresult object updated new thread. "complete" action, when iscompleted set true, original caller expected pass original iasyncresult object corresponding end method returns result. trivial implementation looks follows:

    static func<string> getuser;     public static iasyncresult begingetuser(asynccallback callback, object state)     {         getuser = () =>             {                 thread.sleep(2000);                 return "finished";             };         return getuser.begininvoke(callback, state);     }      public static string endgetuser(iasyncresult asyncresult)     {         return getuser.endinvoke(asyncresult);     } 

the calls might like:

var result = begingetuser(null, null); string value = endgetuser(result); 

of course, trivial case: quote http://kennyw.com/work/indigo/258, "if aren’t doing that’s "natively async", shouldn’t using asyncpattern=true".

fortunately, c# 5.0 or async ctp microsoft released, .net async pattern may become thing of past.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -