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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:26+00:00 2026-05-27T23:00:26+00:00

On ASP.NET MVC, I try to write an async Controller action with the old

  • 0

On ASP.NET MVC, I try to write an async Controller action with the old asynchronous programming model (actually, it is the current one, new one is still a CTP).

Here, I am trying to run 4 operations in parallel and it worked great. Here is the complete code:

public class SampleController : AsyncController {

    public void IndexAsync() {

        AsyncManager.OutstandingOperations.Increment(4);

        var task1 = Task<string>.Factory.StartNew(() => {

            return GetReponse1();
        });
        var task2 = Task<string>.Factory.StartNew(() => {

            return GetResponse2();
        });
        var task3 = Task<string>.Factory.StartNew(() => {

            return GetResponse3();
        });
        var task4 = Task<string>.Factory.StartNew(() => {

            return GetResponse4();
        });

        task1.ContinueWith(t => {

            AsyncManager.Parameters["headers1"] = t.Result;
            AsyncManager.OutstandingOperations.Decrement();
        });

        task2.ContinueWith(t => {

            AsyncManager.Parameters["headers2"] = t.Result;
            AsyncManager.OutstandingOperations.Decrement();
        });

        task3.ContinueWith(t => {

            AsyncManager.Parameters["headers3"] = t.Result;
            AsyncManager.OutstandingOperations.Decrement();
        });

        task4.ContinueWith(t => {

            AsyncManager.Parameters["headers4"] = t.Result;
            AsyncManager.OutstandingOperations.Decrement();
        });

        task3.ContinueWith(t => {

            AsyncManager.OutstandingOperations.Decrement();

        }, TaskContinuationOptions.OnlyOnFaulted);
    }

    public ActionResult IndexCompleted(string headers1, string headers2, string headers3, string headers4) {

        ViewBag.Headers = string.Join("<br/><br/>", headers1, headers2, headers3, headers4);

        return View();
    }

    public ActionResult Index2() {

        ViewBag.Headers = string.Join("<br/><br/>", GetReponse1(), GetResponse2(), GetResponse3(), GetResponse4());

        return View();
    }
}

And these below ones are the methods that the async operations are running:

string GetReponse1() {

    var req = (HttpWebRequest)WebRequest.Create("http://www.twitter.com");
    req.Method = "HEAD";

    var resp = (HttpWebResponse)req.GetResponse();

    return FormatHeaders(resp.Headers);
}

string GetResponse2() {

    var req2 = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com");
    req2.Method = "HEAD";

    var resp2 = (HttpWebResponse)req2.GetResponse();

    return FormatHeaders(resp2.Headers);
}

string GetResponse3() {

    var req = (HttpWebRequest)WebRequest.Create("http://google.com");
    req.Method = "HEAD";

    var resp = (HttpWebResponse)req.GetResponse();

    return FormatHeaders(resp.Headers);
}

string GetResponse4() {

    var req = (HttpWebRequest)WebRequest.Create("http://github.com");
    req.Method = "HEAD";

    var resp = (HttpWebResponse)req.GetResponse();

    return FormatHeaders(resp.Headers);
}

private static string FormatHeaders(WebHeaderCollection headers) {

    var headerStrings = from header in headers.Keys.Cast<string>()
                        select string.Format("{0}: {1}", header, headers[header]);

    return string.Join("<br />", headerStrings.ToArray());
}

I have also Index2 method here which is synchronous and does the same thing.

I compare two operation execution times and there is major difference (approx. 2 seconds)

But I think I am missing lots of things here (exception handling, timeouts, etc). I only implement the exception handling on task3 but I don’t think it is the right way of doing that.

What is the healthiest way of handling exceptions for this kind of operations?

  • 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-05-27T23:00:27+00:00Added an answer on May 27, 2026 at 11:00 pm

    Before accessing the result of a task you should test whether it completed successfully. If there was an error you should not try to access the results but instead do something with the exception:

    task1.ContinueWith(t => 
    {
        if (!t.IsFaulted)
        {
            AsyncManager.Parameters["headers1"] = t.Result;
        }
        else if (t.IsFaulted && t.Exception != null)
        {
            AsyncManager.Parameters["error"] = t.Exception;
        }
        AsyncManager.OutstandingOperations.Decrement();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to use Html.Action in ASP.NET MVC 3 for a controller in
ASP.Net MVC Controllers have several methods of forwarding control to another controller or action.
In my Asp.net MVC app, I have two methods on a controller, one for
I went to http://www.asp.net/mvc/ to try to install ASP.NET MVC. I didn't know, but
I have an embedded database in an asp.net mvc project. If I try to
Ineed to write the Error Handler Helper classes for my asp.net mvc project.. can
i am getting this error on the asp.net mvc default site when i try
ASP.Net MVC 3 RC, Razor Experiencing some unexpected behavior when I try to use
I try to create a asp.net mvc 2 application. My DropDownList won't be validated!
I am following along with the music store example to try learn ASP.NET MVC.

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.