What Asynchronous call in WCF

Last Reply on Aug 21, 2014 01:11 AM By akramkhan0889

Posted on Aug 12, 2014 04:23 AM

Hi

can any one explain me async call in wcf. What is the advantage and how it is used in WCF

Posted on Aug 21, 2014 01:11 AM Modified on on Aug 21, 2014 01:13 AM

Asynchronous service

Say you have a long running web service (say it reads a large file from the file system and does some processing).

If you set this up as a 'synchronous' web service (using the WCF definition of that), then the calling client will have to wait until the processing completes, and typically this will block on one of the asp.net worker threads while processing completes. For a service with high traffic, this can become problematic.

If you set this up as an asynchronous web service, then what you are saying is that your code is going to delegate some of the long running processing to another thread, or use a non-blocking mechanism, and that this will return at some point in the future (if you are using c# 5.0, then you might want to look at examples of the async and await keywords).

For the example, reading a large file could be done using one of the async ReadFile methods.

This will not block one of the asp.net worker threads, allowing potentially greater throughput. The client would typically have to call this service in a different fashion, either using the async keywords, or polling for completion, depending on how it's set up.

(There is often some confusion when people refer to making multiple simultaneous calls to the same service (often via AJAX from a web page) - while the calls from the page will typically be made using an asynchronous mechanism in javascript, this is not quite the same as what is described above - I like to keep a distinction between multiple parallel calls and asynchronous calls in my head)

Asynchronous calls

It's also worth noting that you can make an asynchronous call to a service even if that service is not set up to be 'asynchronous'. This is how AJAX calls in javascript will work, e.g.

var jqxhr = $.ajax( "AnyService.svc" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

alert("Called");

For this example, you would expect to see 'Called' displayed before 'Success', as this will not wait for the service to return prior to proceeding. The service you are calling does not have to be 'asynchronous'.

Edit

As pointed out in the comments, you can also have a client that calls an 'asynchronous' service in a synchronous manner (i.e. the service will not block worker threads for further requests, but the client will block at that side).

 

 

You have two option to use services

1. SOAP SERVICE

2. RESTFULL SERVICES

 

SOAP - Data is being transfered and packed in XML Form

REST- Data is being tranfered and packed in Json Form

 

 

 

EXAMPLE:

e.g. synchronous

When I call you on the phone, I dial your number and WAIT until you pick up. Then you say something, and in the very same moment I listen to you. When you finished, I send you data (talk to you) and in the same moment you receive them (listen to me). At the end of our communication one of us says "END OF TRANSMISSION" (Good Bye), the other says "Acknoledged" (Good Bye) and then both ring off.

asynchronous

I write you a letter. I put it to the postoffice, and it will be sent to you. I the meantime I do NOT WAIT. I do many different other things. Then you receive the letter. You read it while I still do many different other things. Then you write me an answer and send it to me. In all those things I am not involved. At the next day I get a (synchronous) message (a signal) from the system (postman). It (he) says: "Here is a message for you". Alternatively I could poll my inbox every five minutes to check if a new letter is there. Then I pause my other work, receive your letter and read your answer. Then I do something according to this answer. But this are things you will not notice, because you are not involved in what I do with your asynchronous answer.