locked
calling HTTPClient from a web application RRS feed

  • Question

  • User-1100202340 posted

    On ASP.Net WebAPI site there is a sample code that demonstrates invoking a WebAPI using HTTPClient from a console app (link provided below).  The document also cautions that HTTPClient can be instantiated only once for the life of the client application to prevent socket exception.  Is there a simple way to implement this in a ASP.Net MVC or Web Forms application ? (C#) 

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Sunday, June 11, 2017 6:18 PM

All replies

  • User475983607 posted

    See HttpClient on Microsoft docs. The document recommends that you declare HttpClient as static and use the async methods.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.7

    Sunday, June 11, 2017 6:55 PM
  • User1967761114 posted

    Hi vishatasp.net,

    According to your description, HttpClient also could use in MVC and WebForm, and you also could use RestSharp to invoke the Web API, RestSharp is the extend from HttpClient.

    Another generic way in MVC and WebForm is using ajax, you could refer to the following links to learn how to use it.

    Web API & Web Form

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with-aspnet-web-forms

    Web API & MVC

    https://www.codeproject.com/Articles/344078/ASP-NET-WebAPI-Getting-Started-with-MVC-and-WebAP

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Monday, June 12, 2017 9:35 AM
  • User-1100202340 posted

    Thank you MHEBHARD.  It does not explain any where how specifying static or the use of async methods would meet the requirement " HttpClient is intended to be instantiated once and re-used throughout the life of an application." in the context of a web forms or mvc application where we may need to invoke a WebAPI throughout a user session.  The example provided is a console app.  What is the life cycle for a web app that is constantly serving web pages 24/7 and invoking WebAPI to exchange data with the backend ?

    Tuesday, June 13, 2017 2:09 AM
  • User1967761114 posted

    Hi vishatasp.net,

    According your description, I think the correct way of using HttpClient in one application is use one static HttpClient instance across the whole application.

     

    I also find 2 articles that has very detailed description about this problem, you could refer to the following links.

    https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

    https://stackoverflow.com/questions/14075026/httpclient-crawling-results-in-memory-leak

    I think that can reuse one static HttpClient instance as many times and unnecessary create/dispose them all the time.

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Tuesday, June 13, 2017 7:33 AM
  • User475983607 posted

    Thank you MHEBHARD.  It does not explain any where how specifying static or the use of async methods would meet the requirement " HttpClient is intended to be instantiated once and re-used throughout the life of an application." in the context of a web forms or mvc application where we may need to invoke a WebAPI throughout a user session.  The example provided is a console app.  What is the life cycle for a web app that is constantly serving web pages 24/7 and invoking WebAPI to exchange data with the backend ?

    You must not have read the entire document.

    HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly.

    public class GoodController : ApiController  
    {  
        // OK  
        private static readonly HttpClient HttpClient;  
    
        static GoodController()  
        {  
            HttpClient = new HttpClient();  
        }  
    } 

    Plus you can view the HttpClient source code.

    https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpClient.cs

    Tuesday, June 13, 2017 10:09 AM
  • User-1100202340 posted

    Thank you folks for your response

    I will try RestSharp; also try the recommendations per https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ 

    Will provide an update soon

    Best regards

    Monday, June 26, 2017 2:12 AM