{"id":11732,"date":"2022-09-28T09:51:01","date_gmt":"2022-09-28T04:21:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/"},"modified":"2022-09-28T09:51:01","modified_gmt":"2022-09-28T04:21:01","slug":"solved-does-these-httpclient-call-be-called-in-parallel","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/","title":{"rendered":"[Solved] Does these HttpClient call be called in parallel?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-69859015\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"69859015\" data-parentid=\"69836269\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<blockquote>\n<p>Question is: if Test() in my Web App is called 10 times, in parallel, do the POST requests to the remote server are be called in parallel or in serial?<\/p>\n<\/blockquote>\n<p>They will be called  in parallel. Nothing in the code would make it in a blocking way.<\/p>\n<blockquote>\n<p>In fact, when I have huge list of requests, I often receive the message A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.<\/p>\n<\/blockquote>\n<p>There are at least 2 things that need to be fixed here:<\/p>\n<ul>\n<li>Properly using <code>async<\/code>\/<code>await<\/code><\/li>\n<li>Properly using <code>HttpClient<\/code><\/li>\n<\/ul>\n<p>The first one is easy:<\/p>\n<pre><code>public async Task&lt;T&gt; PostRead&lt;T&gt;(string baseAddress, string url, out bool succeded, object entity = null)\n{\n    succeded = false;\n\n    try\n    {\n        using (HttpClient client = new HttpClient())\n        {\n            client.DefaultRequestHeaders.Add(\"Authorization\", MyApiKey);\n            string json = JsonConvert.SerializeObject(entity);\n            var body = new StringContent(json, UnicodeEncoding.UTF8, \"application\/json\");\n\n            var responseMessage = await client.PostAsync($\"{baseAddress}\/{url}\", body);\n            var responseContent = await responseMessage.Content.ReadAsStringAsync();\n\n            if (responseMessage.IsSuccessStatusCode)\n            {\n                succeeded = true;\n                return JsonConvert.DeserializeObject&lt;T&gt;();\n            }\n            else\n            {\n                \/\/ log...\n            }\n        }\n    }\n    catch (Exception ex)\n    {\n        \/\/ log...\n    }\n\n    return default; \/\/ or default(T) depending on the c# version\n}\n<\/code><\/pre>\n<p>The second one is a bit more tricky and can quite possibly be the cause of the problems you&#8217;re seeing. Generally, unless you have some very specific scenario, <code>new HttpClient()<\/code> is plain wrong. It wastes resources, hides dependencies and prevents mocking. The correct way to do this is by using <code>IHttpClientFactory<\/code>, which can be very easily abstracted by using the <code>AddHttpClient<\/code> extension methods when registering the service.<\/p>\n<p>Basically, this would look like this:<\/p>\n<pre><code>services.AddHttpClient&lt;YourClassName&gt;(x =&gt; \n{\n    x.BaseAddress = new Uri(baseAddress);\n    x.DefaultRequestHeaders.Add(\"Authorization\", MyApiKey);\n}\n<\/code><\/pre>\n<p>Then it&#8217;s a matter of getting rid of all <code>using HttpClient<\/code> in the class and just:<\/p>\n<pre><code>private readonly HttpClient _client;\n\npublic MyClassName(HttpClient client) { _client = client; }\n\npublic async Task&lt;T&gt; PostRead&lt;T&gt;(string url, out bool succeded, object entity = null)\n{\n    succeded = false;\n\n    try\n    {\n        string json = JsonConvert.SerializeObject(entity);\n        var responseMessage = await _client.PostAsync($\"{baseAddress}\/{url}\", body);\n        \/\/...\n    }\n    catch (Exception ex)\n    {\n        \/\/ log...\n    }\n\n    return default; \/\/ or default(T) depending on the c# version\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Does these HttpClient call be called in parallel? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Question is: if Test() in my Web App is called 10 times, in parallel, do the POST requests to the remote server are be called in parallel or in serial? They will be called in parallel. Nothing in the code would make it in a blocking way. In fact, when I have huge list &#8230; <a title=\"[Solved] Does these HttpClient call be called in parallel?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\" aria-label=\"More on [Solved] Does these HttpClient call be called in parallel?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[3208,324,2507],"class_list":["post-11732","post","type-post","status-publish","format-standard","hentry","category-solved","tag-asp-net-core-2-2","tag-c","tag-httpclient"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Does these HttpClient call be called in parallel? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Does these HttpClient call be called in parallel? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Question is: if Test() in my Web App is called 10 times, in parallel, do the POST requests to the remote server are be called in parallel or in serial? They will be called in parallel. Nothing in the code would make it in a blocking way. In fact, when I have huge list ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-28T04:21:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Does these HttpClient call be called in parallel?\",\"datePublished\":\"2022-09-28T04:21:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\"},\"wordCount\":213,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"asp.net-core-2.2\",\"c++\",\"httpclient\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\",\"name\":\"[Solved] Does these HttpClient call be called in parallel? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-28T04:21:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Does these HttpClient call be called in parallel?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Does these HttpClient call be called in parallel? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Does these HttpClient call be called in parallel? - JassWeb","og_description":"[ad_1] Question is: if Test() in my Web App is called 10 times, in parallel, do the POST requests to the remote server are be called in parallel or in serial? They will be called in parallel. Nothing in the code would make it in a blocking way. In fact, when I have huge list ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/","og_site_name":"JassWeb","article_published_time":"2022-09-28T04:21:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Does these HttpClient call be called in parallel?","datePublished":"2022-09-28T04:21:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/"},"wordCount":213,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["asp.net-core-2.2","c++","httpclient"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/","url":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/","name":"[Solved] Does these HttpClient call be called in parallel? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-28T04:21:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-does-these-httpclient-call-be-called-in-parallel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Does these HttpClient call be called in parallel?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=11732"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11732\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=11732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=11732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=11732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}