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 7851965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:18:24+00:00 2026-06-02T19:18:24+00:00

I have a WCF service that is in turn calling several other web services.

  • 0

I have a WCF service that is in turn calling several other web services. The services are ordered by preference but called in parallel. Each service will respond with true or false, and when all of the responses have been received, the most preferred positive will be returned to the client.

However, at any point during this, the most preferred service might respond with a positive. At this point it makes no sense to continue querying other services, so I wish to cancel the execution and return the result to the client.

That much I can do:

// Use parallelOptions to store the cancellation token
ParallelOptions po = new ParallelOptions();
po.CancellationToken = cts.Token;
Parallel.ForEach<IntegrationObj>(externalServices, po, x =>
{
    try
    {
        // Send requests
        SendExternalRequest(x);
        po.CancellationToken.ThrowIfCancellationRequested();
    }
    catch (OperationCanceledException e)
    {
        /// Don't throw an exception if the loop was cancelled
    }
    catch (Exception e)
    {
        LogError(e.Message);
    }
});
if (positiveResponses.Count > 0) {
    // Get the most preferred positive response and send it back to the client
    res = positiveResponses.Where(
        x => x.ID == PreferredServiceID)
        .FirstOrDefault();
}
return res;

The above code is a very simplified version of my code, but essentially it checks each response to see if it is currently the most preferred service. If so, it cancels the loop.

However… Cancelling the loop in this way will wait for any services that have already been called to finish before it terminates the loop. There is a reasonable chance that any of the services could time out, which is of course treated as a negative. If just one of those times out, it adds an extra 10 seconds onto my response time.

What I would like to do is one of the following:

  1. Return the preferred service to the client and then continue processing the responses that come in from services that have already started (this is purely for logging purposes so that I know the positive/negative rate of those services).
  2. Cancel the loop instantly and forget about the remaining responses. Ideally I don’t want to do this because it means services that would have responded positive will not be recorded as such, which will affect their preference rating.

So my question is this:

Is what I want to achieve possible? I realise that parallel.foreach might not be the best tool for the job, or that I might not be able to log responses after responding to the client. The most important requirement is that the client receives a fast response without having to wait for services to respond when the “winner” has already been picked.

  • 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-02T19:18:26+00:00Added an answer on June 2, 2026 at 7:18 pm

    The way it’s currently set up is that all your services are started parallel but your check is only programmed to happen after all of them are finished.
    If you want to stick with your foreach approach you could write your if condition inside the foreach , that way if a service finishes first it will directly execute the check on positive responses and you can react accordingly(return the value / kill the others).
    Note that this approach has more thread insafeties which you’ll have to take care off(your reading from a list that one of your services might be trying to write to). It’s also possible that 2 services finish at the same time so your if statement fires twice.

    An alternative for the foreach approach would be to create a thread for each service, put all threads in a list and right after starting all of the threads start a while loop to keep checking(with a sleep off course) for a finished service. This way you can return the value while other threads are still running by breaking the while loop.

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

Sidebar

Related Questions

I have a WCF service that I need to call in a ASP.NET web
I'm have a Silverlight 3 UI that access WCF services which in turn access
I have a WCF web service that is building up data into classes and
I have a WCF service that has a few different responsibilities, but it provides
I have a web client that calls my WCF business service layer, which in
I have been tasked with securing a web service that uses WCF and offers
I have a WCF service that can return several different collections. Objects in each
I have a WCF service that accesses a SQL database to fetch data .
I have a WCF service that I can debug. I put a breakpoint in
I have a WCF service that can return large amount of data depending on

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.