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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:49:04+00:00 2026-05-31T21:49:04+00:00

New ASP.NET Web API HttpClient has been giving me some strange results. Here is

  • 0

New ASP.NET Web API HttpClient has been giving me some strange results. Here is my code:

class Program {

    static async void Main(string[] args) {

        var address = "http://localhost:3895/api/urls";

        Console.WriteLine(await getStringAsync(address));
        Console.ReadLine();

    }

    public static async Task<string> getStringAsync(string uri) {

        var httpClient = new HttpClient();
        return await httpClient.GetStringAsync(uri);
    }
}

This never comes back and the console suddenly appears and disappears. When I change the code as below, it works as it is supposed to:

static void Main(string[] args) {

    var address = "http://localhost:3895/api/urls";

    Console.WriteLine(getString(address));
    Console.ReadLine();

}

public static string getString(string uri) { 

    var httpClient = new HttpClient();

    return httpClient.GetStringAsync(uri).Result;
}

Any idea on what would be the issue?

  • 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-31T21:49:06+00:00Added an answer on May 31, 2026 at 9:49 pm

    async on Main is disallowed in the VS11/.NET 4.5 compiler, so I’m assuming you’re using the Async CTP. If using .NET 4.5 is at all an option, do make the switch.

    That aside, the reason it doesn’t work is because async, or more generally, tasks, rely on being able to signal some way for the remainder of the code to be executed. It works with .Result because the code runs synchronously, so the problem doesn’t apply.

    There is no built-in support for console applications, because they don’t normally use message loops in the way that for example WinForms does, but you can look at Microsoft Visual Studio Async CTP\Samples\(C# Testing) Unit Testing\AsyncTestUtilities, notably GeneralThreadAffineContext.cs, to get a basic example that works in console applications too.

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

Sidebar

Related Questions

I have been asked to migrate some code to our new asp.net web app.
It's been a while since I've created a new ASP.NET web application. (I've been
I have some internal-facing ASP.NET web services that have had numerous API additions over
Using the new ASP.NET Web API beta. I can not seem to get the
I'm creating a web API based on the new ASP.NET Web API. I'm trying
I'm researching the new ASP.NET MVC4 Web API framework. I'm running Visual Studio 2011
I'm converting from the WCF Web API to the new ASP.NET MVC 4 Web
I'm trying to get the new ASP.NET Web API beta (VS 2010 default Web
I create new ASP.NET web application that use SMTP to send message. The problem
I'm trying to start a new ASP.NET Web Application Project using Visual Studio 2008.

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.