Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:59:11+00:00 2026-06-08T04:59:11+00:00

I have a web service in WCF that consume some external web services, so

  • 0

I have a web service in WCF that consume some external web services, so what I want to do is make this service asynchronous in order to release the thread, wait for the completion of all the external services, and then return the result to the client.

With Framework 4.0

public class MyService : IMyService
{
    public IAsyncResult BeginDoWork(int count, AsyncCallback callback, object serviceState)
    {    
        var proxyOne = new Gateway.BackendOperation.BackendOperationOneSoapClient();
        var proxyTwo = new Gateway.BackendOperationTwo.OperationTwoSoapClient();

        var taskOne = Task<int>.Factory.FromAsync(proxyOne.BeginGetNumber, proxyOne.EndGetNumber, 10, serviceState);
        var taskTwo = Task<int>.Factory.FromAsync(proxyTwo.BeginGetNumber, proxyTwo.EndGetNumber, 10, serviceState);
        
        var tasks = new Queue<Task<int>>();
        tasks.Enqueue(taskOne);
        tasks.Enqueue(taskTwo);

        return Task.Factory.ContinueWhenAll(tasks.ToArray(), innerTasks =>
        {
            var tcs = new TaskCompletionSource<int>(serviceState);
            int sum = 0;

            foreach (var innerTask in innerTasks)
            {
                if (innerTask.IsFaulted)
                {
                    tcs.SetException(innerTask.Exception);
                    callback(tcs.Task);
                    return;
                }

                if (innerTask.IsCompleted)
                {
                    sum = innerTask.Result;
                }
            }

            tcs.SetResult(sum);

            callback(tcs.Task);
        });
    }

    public int EndDoWork(IAsyncResult result)
    {
        try
        {
            return ((Task<int>)result).Result;
        }
        catch (AggregateException ex)
        {
            throw ex.InnerException;
        }

    }
}

My questions here are:

  1. This code is using three threads: one that is instanced in the
    BeginDoWork, another one that is instanced when the code enter
    inside the anonymous method ContinueWhenAll, and the last one when
    the callback is executed, in this case EndDoWork. Is that correct or
    I’m doing something wrong on the calls? Should I use any
    synchronization context? Note: The synchronization context is null
    on the main thread.

  2. What happen if I “share” information between
    threads, for instance, the callback function? Will that cause a
    performance issue or the anonymous method is like a closure where I
    can share data?

With Framework 4.5 and Async and Await

Now with Framework 4.5, the code seems too much simple than before:

    public async Task<int> DoWorkAsync(int count)
    {
        var proxyOne = new Backend.ServiceOne.ServiceOneClient();
        var proxyTwo = new Backend.ServiceTwo.ServiceTwoClient();

        var doWorkOne = proxyOne.DoWorkAsync(count);
        var doWorkTwo = proxyTwo.DoWorkAsync(count);

        var result = await Task.WhenAll(doWorkOne, doWorkTwo);

        return doWorkOne.Result + doWorkTwo.Result;
    }

But in this case when I debug the application, I always see that the code is executed on the same thread. So my questions here are:

3.. When I’m waiting for the “awaitable” code, is that thread released and goes back to the thread pool to execute more requests?

3.1. If So, I suppose that when I get a result from the await Task, the execution completes on the same thread that the call before. Is that possible? What happen if that thread is processing another request?

3.2 If Not, how can I release the thread to send it back to the thread pool with Asycn and Await pattern?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:59:14+00:00Added an answer on June 8, 2026 at 4:59 am

    1. This code is using three threads: one that is instanced in the BeginDoWork, another one that is instanced when the code enter inside the anonymous method ContinueWhenAll, and the last one when the callback is executed, in this case EndDoWork. Is that correct or I’m doing something wrong on the calls? Should I use any synchronization context?

    It’s better to think in terms of “tasks” rather than “threads”. You do have three tasks here, each of which will run on the thread pool, one at a time.

    2. What happen if I “share” information between threads, for instance, the callback function? Will that cause a performance issue or the anonymous method is like a closure where I can share data?

    You don’t have to worry about synchronization because each of these tasks can’t run concurrently. BeginDoWork registers the continuation just before returning, so it’s already practically done when the continuation can run. EndDoWork will probably not be called until the continuation is complete; but even if it is, it will block until the continuation is complete.

    (Technically, the continuation can start running before BeginDoWork completes, but BeginDoWork just returns at that point, so it doesn’t matter).

    3. When I’m waiting for the “awaitable” code, is that thread released and goes back to the thread pool to execute more requests?

    Yes.

    3.1. If So, I suppose that when I get a result from the await Task, the execution completes on the same thread that the call before. Is that possible? What happen if that thread is processing another request?

    No. Your host (in this case, ASP.NET) may continue the async methods on any thread it happens to have available.

    This is perfectly safe because only one thread is executing at a time.

    P.S. I recommend

    var result = await Task.WhenAll(doWorkOne, doWorkTwo);
    return result[0] + result[1];
    

    instead of

    var result = await Task.WhenAll(doWorkOne, doWorkTwo);
    return doWorkOne.Result + doWorkTwo.Result;
    

    because Task.Result should be avoided in async programming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET web service (using asmx...have not upgraded to WCF yet) that
I have a WCF web-service and a Silverlight app displaying data from that service.
I have a WCF service that I need to call in a ASP.NET web
I have a web application that communicates to a WCF service through a WCF
I have an aspx web form in a WCF service project that gives a
I have a website that talks to a remote WCF web service. Both use
I have a Linux/c client app that connects to a WCF web service over
I have a complex RIA client that communicates with a WCF SOAP web service,
I have a plugin that exposes a wcf service. if I test this service
So this is my situation. I have to consume a third party web service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.