IDISP014

November 25, 2021 ยท View on GitHub

Use a single instance of HttpClient

TopicValue
IdIDISP014
SeverityInfo
EnabledTrue
CategoryIDisposableAnalyzers.Correctness
CodeCreationAnalyzer

Description

Use a single instance of HttpClient.

Motivation

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

And:

http://byterot.blogspot.se/2016/07/singleton-httpclient-dns.html

How to fix violations

private static HttpClient Client =new HttpClient { ... };

Configure severity

Via ruleset file.

Configure the severity per project, for more info see MSDN.

Via #pragma directive.

#pragma warning disable IDISP014 // Use a single instance of HttpClient
Code violating the rule here
#pragma warning restore IDISP014 // Use a single instance of HttpClient

Or put this at the top of the file to disable all instances.

#pragma warning disable IDISP014 // Use a single instance of HttpClient

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness", 
    "IDISP014:Use a single instance of HttpClient", 
    Justification = "Reason...")]