Home
%3CLINGO-SUB%20id%3D%22lingo-sub-709038%22%20slang%3D%22en-US%22%3ESystem.Net.HttpClient%20Design%20Guidance%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-709038%22%20slang%3D%22en-US%22%3E%3CH1%20id%3D%22toc-hId-1984508630%22%20id%3D%22toc-hId-1984508631%22%3EDesign%20Pattern%20Discussion%3C%2FH1%3E%0A%3CH2%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B28%2C48%2C2%2C2%5D%7D%22%20id%3D%22toc-hId--764161836%22%20id%3D%22toc-hId--764161835%22%3EInitial%20Problems%3C%2FH2%3E%0A%3CH2%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B28%2C48%2C2%2C2%5D%7D%22%20id%3D%22toc-hId-978648499%22%20id%3D%22toc-hId-978648500%22%3ERecommendations%3C%2FH2%3E%0A%3CP%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B75%2C271%2C0%2C0%5D%7D%22%3Edocs.microsoft.com%20makes%20an%20important%20observation%20about%20HttpClient%20usage%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.net.http.httpclient%3Fview%3Dnetframework-4.7.2%22%20data-parsoid%3D%22%7B%26quot%3BtargetOff%26quot%3B%3A244%2C%26quot%3BcontentOffsets%26quot%3B%3A%5B244%2C270%5D%2C%26quot%3Bdsr%26quot%3B%3A%5B148%2C271%2C96%2C1%5D%7D%22%20data-ve-attributes%3D%22%7B%26quot%3Brel%26quot%3B%3A%26quot%3Bmw%3AExtLink%26quot%3B%7D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ESystem.Net.Http.HttpClient%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B274%2C583%2C2%2C2%5D%7D%22%3EHttpClient%20is%20intended%20to%20be%20instantiated%20once%20and%20re-used%20throughout%20the%20life%20of%20an%20application.%20Instantiating%20an%20HttpClient%20class%20for%20every%20request%20will%20exhaust%20the%20number%20of%20sockets%20available%20under%20heavy%20loads.%20This%20will%20result%20in%20SocketException%20errors.%20Below%20is%20an%20example%20using%20HttpClient%20correctly.%3C%2FEM%3E%3C%2FP%3E%0A%3CH3%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B584%2C607%2C3%2C3%5D%7D%22%20id%3D%22toc-hId--1770021967%22%20id%3D%22toc-hId--1770021966%22%3EWhat%20Does%20It%20Mean%3F%3C%2FH3%3E%0A%3CP%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B608%2C1017%2C0%2C0%5D%7D%22%3EThe%20language%20%E2%80%98%3CEM%3Einstantiated%20once%20and%20re-used%E2%80%99%3C%2FEM%3E%20invokes%20in%20the%20developer%20mind%2C%20multiple%20potential%20solutions.%20However%2C%20the%20example%20provided%20in%20the%20documentation%20may%20unintentionally%20mislead%20developers%20into%20making%20a%20poor%20design%20choice.%20Take%20for%20example%20the%20following%20code%20sample%20which%20describes%20a%20design%20pattern%20which%20is%20in%20fact%2C%20not%20recommend%3A%3C%2FP%3E%0A%3CH3%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1019%2C1069%2C3%2C3%5D%7D%22%20id%3D%22toc-hId--27211632%22%20id%3D%22toc-hId--27211631%22%3EExample%201%3A%20Directly%20contradicts%20our%20published%20guidance%3C%2FH3%3E%0A%3CPRE%3Estatic%20async%20Task%20Main()%0A%7B%0A%20%20%20%2F%2F%20Create%20a%20New%20HttpClient%20object%20and%20dispose%20it%20when%20done%2C%20so%20the%20app%20doesn't%20leak%20resources%0A%20%20%20using%20(HttpClient%20client%20%3D%20new%20HttpClient())%0A%20%20%20%7B%0A%20%20%20%20%20%2F%2FThis%20breaks%20the%20guidance%20given%20already%20as%20it%20does%20not%20create%20a%20single%20instance%20once%20and%20re-use.%20Underload%20this%20may%20cause%20resource%20issues%20with%20the%20application.%0A%20%20%20%7D%0A%7D%0A%3C%2FPRE%3E%0A%3CP%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1468%2C1656%2C0%2C0%5D%7D%22%3E%3CSPAN%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3EOur%20next%20example%20resolves%20one%20part%20of%20our%20recommendations%3A%20creating%20a%20single%20instance%20but%20fails%20to%20advise%20us%20on%20the%20practical%20situation%20the%20HttpClient%20is%20called%20regularly%20from%20many%20threads%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH3%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1658%2C1713%2C3%2C3%5D%7D%22%20id%3D%22toc-hId-1715598703%22%20id%3D%22toc-hId-1715598704%22%3EExample%202%3A%20Solves%20one%20problem%3B%20introduces%20another%3C%2FH3%3E%0A%3CPRE%3Epublic%20class%20GoodController%20%3A%20ApiController%0A%7B%0A%20%20%20%20%2F%2F%20OK%0A%20%20%20%20private%20static%20readonly%20HttpClient%20HttpClient%3B%0A%0A%20%20%20%20static%20GoodController()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20HttpClient%20%3D%20new%20HttpClient()%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FPRE%3E%0A%3CP%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1952%2C2587%2C0%2C0%5D%7D%22%3E%3CSPAN%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3EThis%20solution%20may%20not%20be%20the%20best%20for%20applications%20making%20multiple%20calls%20using%20the%20same%20instance%20of%20HttpClient%20from%20several%20threads.%20For%20example%2C%20one%20issue%20that%20arises%20using%20this%20approach%20could%20be%20that%20several%20threads%20contend%20for%20the%20default%20headers%20collection%20of%20the%20same%20instance%20of%20the%20HttpClient%20class%20and%20create%20a%20high%20CPU%20condition.%20Using%20a%20single%20static%20instance%20of%20HttpClient%20will%20not%20fix%20the%20situation.%20The%20developer%20requires%20a%20different%20solution.%20At%20this%20point%20in%20the%20discussion%20the%20developer%20may%20start%20to%20read%20about%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.net.http.httpclienthandler%3Fview%3Dnetframework-4.7.2%22%20data-parsoid%3D%22%7B%26quot%3BtargetOff%26quot%3B%3A2561%2C%26quot%3BcontentOffsets%26quot%3B%3A%5B2561%2C2578%5D%2C%26quot%3Bdsr%26quot%3B%3A%5B2458%2C2579%2C103%2C1%5D%7D%22%20data-ve-attributes%3D%22%7B%26quot%3Brel%26quot%3B%3A%26quot%3Bmw%3AExtLink%26quot%3B%7D%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EHttpClientHandler%3C%2FA%3E%20class.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B2589%2C2610%2C2%2C2%5D%7D%22%20id%3D%22toc-hId--640044753%22%20id%3D%22toc-hId--640044752%22%3EHttpClientHandler%3C%2FH2%3E%0A%3CP%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B2611%2C2916%2C0%2C0%5D%7D%22%3EHttpClientHandler%20is%20responsible%20for%20managing%20the%20underlying%20connection%20resources%20of%20the%20HttpClient%20instances.%20Again%2C%20looking%20into%20the%20documentation%20examples%20we%20do%20not%20have%20any%20improved%20details%20for%20handling%20creating%20a%20single%20instance%20and%20re-usability%20with%20HttpClient%3A%3C%2FP%3E%0A%3CH3%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B2917%2C2947%2C3%2C3%5D%7D%22%20id%3D%22toc-hId-906252077%22%20id%3D%22toc-hId-906252078%22%3EMore%20Misleading%20Examples%3C%2FH3%3E%0A%3CPRE%3Estatic%20async%20Task%20Main()%0A%7B%0A%20%20%20%2F%2F%20Create%20an%20HttpClientHandler%20object%20and%20set%20to%20use%20default%20credentials%0A%20%20%20HttpClientHandler%20handler%20%3D%20new%20HttpClientHandler()%3B%20%2F%2Fonce%20again%20we%20create%20a%20new%20instance%20each%20time%20this%20method%20is%20called%0A%20%20%20handler.UseDefaultCredentials%20%3D%20true%3B%0A%0A%20%20%20%2F%2F%20Create%20an%20HttpClient%20object%0A%20%20%20HttpClient%20client%20%3D%20new%20HttpClient(handler)%3B%20%2F%2Fand%20we%20continue%20to%20avoid%20following%20our%20recommendations%0A%7D%0A%3C%2FPRE%3E%0A%3CH3%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B3408%2C3432%2C3%2C3%5D%7D%22%20id%3D%22toc-hId--1645904884%22%20id%3D%22toc-hId--1645904883%22%3E%3CSPAN%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3ETowards%20A%20Solution%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B3433%2C3856%2C0%2C0%5D%7D%22%3EWhat%20then%20is%20a%20good%20way%20to%20both%20share%20the%20HttpClient%20resources%20to%20enable%20the%20high%20scalability%20and%20performance%20while%20not%20exhausting%20system%20resources%20but%20avoiding%20the%20problems%20that%20come%20from%20using%20the%20same%20instance%20of%20HttpClient%20from%20several%20threads%3F%20One%20answer%20is%20using%20a%20shared%20instance%20of%20HttpClientHandler%20which%20is%20passed%20into%20an%20instance%20of%20HttpClient%20and%20informing%20the%20client%20that%20it%20does%20not%20own%20the%20underlying%20handler.%3C%2FP%3E%0A%3CH3%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B3858%2C3887%2C3%2C3%5D%7D%22%20id%3D%22toc-hId-96905451%22%20id%3D%22toc-hId-96905452%22%3EExample%203%3A%20One%20solution%3C%2FH3%3E%0A%3CPRE%3Epublic%20static%20System.Net.Http.HttpClientHandler%20GetHttpClientHandler()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(HttpClientHandler%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20HttpClientHandler%20%3D%20new%20System.Net.Http.HttpClientHandler()%20%7B%20MaxConnectionsPerServer%20%3D%2020%20%7D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20HttpClientHandler%3B%0A%20%20%20%20%20%20%20%20%7D%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B4597%2C5339%2C0%2C0%5D%7D%22%3E%3CSPAN%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3EThis%20approach%20resolves%20the%20single%20instance%20for%20the%20lifetime%20of%20the%20application%20and%20still%20allows%20for%20scalability%20and%20accessibility%20from%20several%20threads.%20And%20it%20opens%20the%20door%20into%20finding%20solutions%20for%20other%20problems%20with%20the%20HttpClient%20such%20as%2C%20how%20to%20properly%20deal%20with%20DNS%20changes%20after%20the%20client%20has%20been%20instantiated.%20Without%20a%20way%20of%20cleaning%20up%20a%20HttpClient%20or%20HttpClientHandler%20after%20its%20creation%20-%20the%20moment%20the%20client%20goes%20bad%2C%20if%20it's%20not%20recreated%20then%20the%20application%20will%20be%20stuck%20until%20recycled.%20This%20is%20a%20topic%20for%20another%20chapter%20but%20here%20you%20can%20see%20that%20using%20the%20underlying%20handler%20will%20expose%20a%20way%20to%20trap%20for%20errors%20and%20signal%20to%20recreate%20the%20handler%2C%20which%20will%20be%20automatically%20used%20on%20the%20next%20HttpClient%20calls...%20%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-709038%22%20slang%3D%22en-US%22%3E%3CH1%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2030px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%2024px%200px%2012px%200px%3B%22%20id%3D%22toc-hId-423489920%22%3EDesign%20Pattern%20Discussion%3C%2FH1%3E%0A%3CH2%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2024px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B28%2C48%2C2%2C2%5D%7D%22%20id%3D%22toc-hId-369348896%22%3EInitial%20Problems%3C%2FH2%3E%0A%3CH2%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2024px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B28%2C48%2C2%2C2%5D%7D%22%20id%3D%22toc-hId-2112159231%22%3ERecommendations%3C%2FH2%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B75%2C271%2C0%2C0%5D%7D%22%3Edocs.microsoft.com%20makes%20an%20important%20observation%20about%20HttpClient%20usage%20%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20text-decoration%3A%20underline%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.net.http.httpclient%3Fview%3Dnetframework-4.7.2%22%20data-parsoid%3D%22%7B%26quot%3BtargetOff%26quot%3B%3A244%2C%26quot%3BcontentOffsets%26quot%3B%3A%5B244%2C270%5D%2C%26quot%3Bdsr%26quot%3B%3A%5B148%2C271%2C96%2C1%5D%7D%22%20data-ve-attributes%3D%22%7B%26quot%3Brel%26quot%3B%3A%26quot%3Bmw%3AExtLink%26quot%3B%7D%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3ESystem.Net.Http.HttpClient%3C%2FA%3E%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3E%3CEM%20style%3D%22box-sizing%3A%20border-box%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B274%2C583%2C2%2C2%5D%7D%22%3EHttpClient%20is%20intended%20to%20be%20instantiated%20once%20and%20re-used%20throughout%20the%20life%20of%20an%20application.%20Instantiating%20an%20HttpClient%20class%20for%20every%20request%20will%20exhaust%20the%20number%20of%20sockets%20available%20under%20heavy%20loads.%20This%20will%20result%20in%20SocketException%20errors.%20Below%20is%20an%20example%20using%20HttpClient%20correctly.%3C%2FEM%3E%3C%2FP%3E%0A%3CH3%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B584%2C607%2C3%2C3%5D%7D%22%20id%3D%22toc-hId-2058018207%22%3EWhat%20Does%20It%20Mean%3F%3C%2FH3%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B608%2C1017%2C0%2C0%5D%7D%22%3EThe%20language%20%E2%80%98%3CEM%20style%3D%22box-sizing%3A%20border-box%3B%22%3Einstantiated%20once%20and%20re-used%E2%80%99%3C%2FEM%3E%20invokes%20in%20the%20developer%20mind%2C%20multiple%20potential%20solutions.%20However%2C%20the%20example%20provided%20in%20the%20documentation%20may%20unintentionally%20mislead%20developers%20into%20making%20a%20poor%20design%20choice.%20Take%20for%20example%20the%20following%20code%20sample%20which%20describes%20a%20design%20pattern%20which%20is%20in%20fact%2C%20not%20recommend%3A%3C%2FP%3E%0A%3CH3%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1019%2C1069%2C3%2C3%5D%7D%22%20id%3D%22toc-hId--494138754%22%3EExample%201%3A%20Directly%20contradicts%20our%20published%20guidance%3C%2FH3%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f5f5f5%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%233e3e3e%3B%20display%3A%20block%3B%20font-family%3A%20Menlo%2CMonaco%2CConsolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Cmonospace%3B%20font-size%3A%2013px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20overflow-wrap%3A%20break-word%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20pre%3B%20word-break%3A%20break-all%3B%20word-spacing%3A%200px%3B%20padding%3A%2011.5px%3B%20margin%3A%200px%200px%2012px%200px%3B%20border%3A%201px%20solid%20%23cccccc%3B%22%3Estatic%20async%20Task%20Main()%0A%7B%0A%20%20%20%2F%2F%20Create%20a%20New%20HttpClient%20object%20and%20dispose%20it%20when%20done%2C%20so%20the%20app%20doesn't%20leak%20resources%0A%20%20%20using%20(HttpClient%20client%20%3D%20new%20HttpClient())%0A%20%20%20%7B%0A%20%20%20%20%20%2F%2FThis%20breaks%20the%20guidance%20given%20already%20as%20it%20does%20not%20create%20a%20single%20instance%20once%20and%20re-use.%20Underload%20this%20may%20cause%20resource%20issues%20with%20the%20application.%0A%20%20%20%7D%0A%7D%0A%3C%2FPRE%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1468%2C1656%2C0%2C0%5D%7D%22%3E%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20font-family%3A%20%26amp%3Bquot%3B%22%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3EOur%20next%20example%20resolves%20one%20part%20of%20our%20recommendations%3A%20creating%20a%20single%20instance%20but%20fails%20to%20advise%20us%20on%20the%20practical%20situation%20the%20HttpClient%20is%20called%20regularly%20from%20many%20threads%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH3%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1658%2C1713%2C3%2C3%5D%7D%22%20id%3D%22toc-hId-1248671581%22%3EExample%202%3A%20Solves%20one%20problem%3B%20introduces%20another%3C%2FH3%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f5f5f5%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%233e3e3e%3B%20display%3A%20block%3B%20font-family%3A%20Menlo%2CMonaco%2CConsolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Cmonospace%3B%20font-size%3A%2013px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20overflow-wrap%3A%20break-word%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20pre%3B%20word-break%3A%20break-all%3B%20word-spacing%3A%200px%3B%20padding%3A%2011.5px%3B%20margin%3A%200px%200px%2012px%200px%3B%20border%3A%201px%20solid%20%23cccccc%3B%22%3Epublic%20class%20GoodController%20%3A%20ApiController%0A%7B%0A%20%20%20%20%2F%2F%20OK%0A%20%20%20%20private%20static%20readonly%20HttpClient%20HttpClient%3B%0A%0A%20%20%20%20static%20GoodController()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20HttpClient%20%3D%20new%20HttpClient()%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FPRE%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B1952%2C2587%2C0%2C0%5D%7D%22%3E%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20font-family%3A%20%26amp%3Bquot%3B%22%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3EThis%20solution%20may%20not%20be%20the%20best%20for%20applications%20making%20multiple%20calls%20using%20the%20same%20instance%20of%20HttpClient%20from%20several%20threads.%20For%20example%2C%20one%20issue%20that%20arises%20using%20this%20approach%20could%20be%20that%20several%20threads%20contend%20for%20the%20default%20headers%20collection%20of%20the%20same%20instance%20of%20the%20HttpClient%20class%20and%20create%20a%20high%20CPU%20condition.%20Using%20a%20single%20static%20instance%20of%20HttpClient%20will%20not%20fix%20the%20situation.%20The%20developer%20requires%20a%20different%20solution.%20At%20this%20point%20in%20the%20discussion%20the%20developer%20may%20start%20to%20read%20about%20the%20%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20text-decoration%3A%20underline%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.net.http.httpclienthandler%3Fview%3Dnetframework-4.7.2%22%20data-parsoid%3D%22%7B%26quot%3BtargetOff%26quot%3B%3A2561%2C%26quot%3BcontentOffsets%26quot%3B%3A%5B2561%2C2578%5D%2C%26quot%3Bdsr%26quot%3B%3A%5B2458%2C2579%2C103%2C1%5D%7D%22%20data-ve-attributes%3D%22%7B%26quot%3Brel%26quot%3B%3A%26quot%3Bmw%3AExtLink%26quot%3B%7D%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EHttpClientHandler%3C%2FA%3E%20class.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2024px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B2589%2C2610%2C2%2C2%5D%7D%22%20id%3D%22toc-hId-493465979%22%3EHttpClientHandler%3C%2FH2%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B2611%2C2916%2C0%2C0%5D%7D%22%3EHttpClientHandler%20is%20responsible%20for%20managing%20the%20underlying%20connection%20resources%20of%20the%20HttpClient%20instances.%20Again%2C%20looking%20into%20the%20documentation%20examples%20we%20do%20not%20have%20any%20improved%20details%20for%20handling%20creating%20a%20single%20instance%20and%20re-usability%20with%20HttpClient%3A%3C%2FP%3E%0A%3CH3%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B2917%2C2947%2C3%2C3%5D%7D%22%20id%3D%22toc-hId-439324955%22%3EMore%20Misleading%20Examples%3C%2FH3%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f5f5f5%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%233e3e3e%3B%20display%3A%20block%3B%20font-family%3A%20Menlo%2CMonaco%2CConsolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Cmonospace%3B%20font-size%3A%2013px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20overflow-wrap%3A%20break-word%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20pre%3B%20word-break%3A%20break-all%3B%20word-spacing%3A%200px%3B%20padding%3A%2011.5px%3B%20margin%3A%200px%200px%2012px%200px%3B%20border%3A%201px%20solid%20%23cccccc%3B%22%3Estatic%20async%20Task%20Main()%0A%7B%0A%20%20%20%2F%2F%20Create%20an%20HttpClientHandler%20object%20and%20set%20to%20use%20default%20credentials%0A%20%20%20HttpClientHandler%20handler%20%3D%20new%20HttpClientHandler()%3B%20%2F%2Fonce%20again%20we%20create%20a%20new%20instance%20each%20time%20this%20method%20is%20called%0A%20%20%20handler.UseDefaultCredentials%20%3D%20true%3B%0A%0A%20%20%20%2F%2F%20Create%20an%20HttpClient%20object%0A%20%20%20HttpClient%20client%20%3D%20new%20HttpClient(handler)%3B%20%2F%2Fand%20we%20continue%20to%20avoid%20following%20our%20recommendations%0A%7D%0A%3C%2FPRE%3E%0A%3CH3%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B3408%2C3432%2C3%2C3%5D%7D%22%20id%3D%22toc-hId--2112832006%22%3E%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20font-family%3A%20%26amp%3Bquot%3B%22%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3ETowards%20A%20Solution%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B3433%2C3856%2C0%2C0%5D%7D%22%3EWhat%20then%20is%20a%20good%20way%20to%20both%20share%20the%20HttpClient%20resources%20to%20enable%20the%20high%20scalability%20and%20performance%20while%20not%20exhausting%20system%20resources%20but%20avoiding%20the%20problems%20that%20come%20from%20using%20the%20same%20instance%20of%20HttpClient%20from%20several%20threads%3F%20One%20answer%20is%20using%20a%20shared%20instance%20of%20HttpClientHandler%20which%20is%20passed%20into%20an%20instance%20of%20HttpClient%20and%20informing%20the%20client%20that%20it%20does%20not%20own%20the%20underlying%20handler.%3C%2FP%3E%0A%3CH3%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20inherit%3B%20font-family%3A%20inherit%3B%20font-size%3A%2018px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20normal%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.2%3B%20margin-bottom%3A%2012px%3B%20margin-top%3A%2024px%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B3858%2C3887%2C3%2C3%5D%7D%22%20id%3D%22toc-hId--370021671%22%3EExample%203%3A%20One%20solution%3C%2FH3%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f5f5f5%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%233e3e3e%3B%20display%3A%20block%3B%20font-family%3A%20Menlo%2CMonaco%2CConsolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Cmonospace%3B%20font-size%3A%2013px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20overflow%3A%20auto%3B%20overflow-wrap%3A%20break-word%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20pre%3B%20word-break%3A%20break-all%3B%20word-spacing%3A%200px%3B%20padding%3A%2011.5px%3B%20margin%3A%200px%200px%2012px%200px%3B%20border%3A%201px%20solid%20%23cccccc%3B%22%3Epublic%20static%20System.Net.Http.HttpClientHandler%20GetHttpClientHandler()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(HttpClientHandler%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20HttpClientHandler%20%3D%20new%20System.Net.Http.HttpClientHandler()%20%7B%20MaxConnectionsPerServer%20%3D%2020%20%7D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20HttpClientHandler%3B%0A%20%20%20%20%20%20%20%20%7D%0A%3C%2FPRE%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%20margin%3A%200px%3B%22%20data-parsoid%3D%22%7B%26quot%3Bdsr%26quot%3B%3A%5B4597%2C5339%2C0%2C0%5D%7D%22%3E%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20font-family%3A%20%26amp%3Bquot%3B%22%20data-ve-attributes%3D%22%7B%26quot%3Babout%26quot%3B%3A%26quot%3B%23mwt3%26quot%3B%7D%22%3EThis%20approach%20resolves%20the%20single%20instance%20for%20the%20lifetime%20of%20the%20application%20and%20still%20allows%20for%20scalability%20and%20accessibility%20from%20several%20threads.%20And%20it%20opens%20the%20door%20into%20finding%20solutions%20for%20other%20problems%20with%20the%20HttpClient%20such%20as%2C%20how%20to%20properly%20deal%20with%20DNS%20changes%20after%20the%20client%20has%20been%20instantiated.%20Without%20a%20way%20of%20cleaning%20up%20a%20HttpClient%20or%20HttpClientHandler%20after%20its%20creation%20-%20the%20moment%20the%20client%20goes%20bad%2C%20if%20it's%20not%20recreated%20then%20the%20application%20will%20be%20stuck%20until%20recycled.%20This%20is%20a%20topic%20for%20another%20chapter%20but%20here%20you%20can%20see%20that%20using%20the%20underlying%20handler%20will%20expose%20a%20way%20to%20trap%20for%20errors%20and%20signal%20to%20recreate%20the%20handler%2C%20which%20will%20be%20automatically%20used%20on%20the%20next%20HttpClient%20calls...%20%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-709038%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Etdevere%40microsoft.com%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Design Pattern Discussion

Initial Problems

Recommendations

docs.microsoft.com makes an important observation about HttpClient usage System.Net.Http.HttpClient

 

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.

What Does It Mean?

The language ‘instantiated once and re-used’ invokes in the developer mind, multiple potential solutions. However, the example provided in the documentation may unintentionally mislead developers into making a poor design choice. Take for example the following code sample which describes a design pattern which is in fact, not recommend:

Example 1: Directly contradicts our published guidance

static async Task Main()
{
   // Create a New HttpClient object and dispose it when done, so the app doesn't leak resources
   using (HttpClient client = new HttpClient())
   {
     //This breaks the guidance given already as it does not create a single instance once and re-use. Underload this may cause resource issues with the application.
   }
}

Our next example resolves one part of our recommendations: creating a single instance but fails to advise us on the practical situation the HttpClient is called regularly from many threads

Example 2: Solves one problem; introduces another

public class GoodController : ApiController
{
    // OK
    private static readonly HttpClient HttpClient;

    static GoodController()
    {
        HttpClient = new HttpClient();
    }
}

This solution may not be the best for applications making multiple calls using the same instance of HttpClient from several threads. For example, one issue that arises using this approach could be that several threads contend for the default headers collection of the same instance of the HttpClient class and create a high CPU condition. Using a single static instance of HttpClient will not fix the situation. The developer requires a different solution. At this point in the discussion the developer may start to read about the HttpClientHandler class.

HttpClientHandler

HttpClientHandler is responsible for managing the underlying connection resources of the HttpClient instances. Again, looking into the documentation examples we do not have any improved details for handling creating a single instance and re-usability with HttpClient:

More Misleading Examples

static async Task Main()
{
   // Create an HttpClientHandler object and set to use default credentials
   HttpClientHandler handler = new HttpClientHandler(); //once again we create a new instance each time this method is called
   handler.UseDefaultCredentials = true;

   // Create an HttpClient object
   HttpClient client = new HttpClient(handler); //and we continue to avoid following our recommendations
}

Towards A Solution

What then is a good way to both share the HttpClient resources to enable the high scalability and performance while not exhausting system resources but avoiding the problems that come from using the same instance of HttpClient from several threads? One answer is using a shared instance of HttpClientHandler which is passed into an instance of HttpClient and informing the client that it does not own the underlying handler.

Example 3: One solution

public static System.Net.Http.HttpClientHandler GetHttpClientHandler()
        {
            if (HttpClientHandler == null)
            {
                HttpClientHandler = new System.Net.Http.HttpClientHandler() { MaxConnectionsPerServer = 20 };
                
            }
            return HttpClientHandler;
        }

       

This approach resolves the single instance for the lifetime of the application and still allows for scalability and accessibility from several threads. And it opens the door into finding solutions for other problems with the HttpClient such as, how to properly deal with DNS changes after the client has been instantiated. Without a way of cleaning up a HttpClient or HttpClientHandler after its creation - the moment the client goes bad, if it's not recreated then the application will be stuck until recycled. This is a topic for another chapter but here you can see that using the underlying handler will expose a way to trap for errors and signal to recreate the handler, which will be automatically used on the next HttpClient calls...