Sunil
Sunil

Reputation: 21406

Calling WCF in async manner from ASP.Net will return worker thread to thread pool of ASP.Net?

I am trying to make my ASP.Net website more scalable, but not sure if the ASP.Net thread receiving the web request is returned to thread pool before the async call to WCF method completes Or this thread will wait for async call to finish.

The async call to WCF method takes about 20 to 40 seconds to complete.

Edit 1 : My code is as below for the page code-behind from where I am calling an async WCF method. The WCF method is async on WCF side, and it is also being called in an async manner from this page code-behind.

    protected void Page_Load(object sender, EventArgs e)
    {
        using (ABCService.ServiceClient sc = new ABCService.ServiceClient())
        {
           // List<ABCService.Product> products = sc.GetDocSummary("Vend1", null, false);//this is synchronous call from client
          sc.BeginGetProducts("Vend1",GetProductsCallback, sc);//this is asynchronous call from WCF
        }
    }

    protected void GetProductsCallback(IAsyncResult asyncResult)
    {
        List<ABCService.Product> products = ((ABCService.ServiceClient)asyncResult.AsyncState).EndGetProducts(asyncResult); //this will call the WCF EndGetProducts method
    }

Upvotes: 2

Views: 1983

Answers (2)

Sunil
Sunil

Reputation: 21406

I found the answer which is explained in detail below.

I found that the threadid given by System.Threading.Thread.CurrentThread.ManagedThreadId was different for 'sc.BeginGetProducts' method as compared to the threadid for method 'GetProductsCallback'.

So it seems logical to conclude that the original ASP.Net worker thread was being returned to the thread pool, and a new thread from thread pool was being used for callback.

However, even though the original thread is returned to thread pool, this does not mean that the page will render on browser-side. The page will render only after the callback method gets executed.

I initially thought that the page would render on browser-side after an async call is made but before the callback method is called, since the UI should not be blocking in case of async WCF calls. Here, its more appropriate to say that the 'worker thread (and not UI) is non-blocking when making async WCF calls from ASP.Net app'.

Another interesting feature I found was that even when Page is NOT Async i.e. Async="false" in page directive, I still saw two different thread ids being used for callback method and the original WCF call. So we still get the non-blocking benefit of using asyn WCF methods in our normal non-async pages.

Upvotes: 1

Grzegorz W
Grzegorz W

Reputation: 3517

Yes your thread will be returned to the thread pool because your request will finish after sc.BeginGetProducts. Method GetProductsCallback will not have input on request being processed.

You should read my answer to this question. It's similar to Your problem.

In short: to achieve what You want you must use asynchronous web pages. If You are using .Net 4.5 You should loot at this article.

Upvotes: 1

Related Questions