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

The Archive Base Latest Questions

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

I am calling an external service using HttpClient from within an ASP.Net MVC 4

  • 0

I am calling an external service using HttpClient from within an ASP.Net MVC 4 Web Api project running on .Net Framework 4.5

The sample code is as follows (ignore the return values as this is sample code to test calling an external service):

public class ValuesController : ApiController
{
    static string _address = "http://api.worldbank.org/countries?format=json";
    private string result;

    // GET api/values
    public IEnumerable<string> Get()
    {
        GetResponse();
        return new string[] { result, "value2" };
    }

    private async void GetResponse()
    {
        var client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(_address);
        response.EnsureSuccessStatusCode();
        result = await response.Content.ReadAsStringAsync();
    }
}

While the code in the private method does indeed work the problem I have is that the Controller Get() calls the GetResponse() but it is not awaiting the result but instead immediately executes the return with result = null.

I have also tried using a simpler synchronous call with a WebClient as follows:

 // GET api/values
    public IEnumerable<string> Get()
    {
        //GetResponse();

        var client = new WebClient();

        result = client.DownloadString(_address);

        return new string[] { result, "value2" };
    }

which works fine.

What am I doing wrong? Why does the Get() not await the private method completion in the async sample?

  • 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-13T19:13:25+00:00Added an answer on June 13, 2026 at 7:13 pm

    Aha, I needed to do the following (return a Task rather then void):

     // GET api/values
        public async Task<IEnumerable<string>> Get()
        {
            var result = await GetExternalResponse();
    
            return new string[] { result, "value2" };
        }
    
        private async Task<string> GetExternalResponse()
        {
            var client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(_address);
            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsStringAsync();
            return result;
        }
    

    Also I hadn’t realised I could mark the Get() operation as async which is what allowed me to await the external call.

    Thanks to Stephen Cleary for his blog post Async and Await which pointed me in the right direction.

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

Sidebar

Related Questions

The scenario is around calling an external SSL SOAP web service from within Mirth.
I have a problem when calling a method from an external web service. The
Say I have an application with the following structure. // ASP.NET MVC (Web Project)
I am calling an external REST service (Vimeo REST API). The response of the
I'm building a .NET component that will call an external web service. I used
While calling from my Web Application, WCF response i am getting is : <s:Envelope
I would like to implement an API for my web based service. I've been
I'm creating an MVC website and also intend to create a web API for
I understand using subprocess is the preferred way of calling external command. But what
There is an external Web service, respectively, requires the client to him. Based 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.