Started getting this error after updating C# upgraded from.NET 5 to 6-
Warning SYSLIB0014 'ServicePointManager.FindServicePoint(Uri)' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.'
var servicePoint = ServicePointManager.FindServicePoint(requestUri.GetEndpoint());
if (servicePoint.ConnectionLeaseTimeout == -1){}
On the Networking of theBreaking changes in .NET 6 there's WebRequest, WebClient, and ServicePoint are obsolete :
WebRequest, WebClient, and ServicePoint are obsolete
xref:System.Net.WebRequest, xref:System.Net.WebClient, and xref:System.Net.ServicePoint classes are marked as obsolete and generate a
SYSLIB0014
warning at compile time.Version introduced
6.0
Change description
WebRequest, WebClient, and ServicePoint classes were added to .NET Core in version 2.0 for backward compatibility. However, they introduced several runtime breaking changes, for example,
WebRequest.GetRequestStream
allocates memory for the whole response, andWebClient.CancelAsync
doesn't always cancel immediately.Starting in .NET 6, the WebRequest, WebClient, and ServicePoint classes are deprecated. The classes are still available, but they're not recommended for new development. To reduce the number of analyzer warnings, only construction methods are decorated with the ObsoleteAttribute attribute.
Recommended action
Use the System.Net.Http.HttpClient class instead.
For FTP, since HttpClient doesn't support it, we recommend using a third-party library.
Affected APIs
- WebRequest
- HttpWebRequest
- FtpWebRequest
- WebClient
- ServicePoint
We can replace ServicePointManager.FindServicePoint
with SocketsHttpHandler
as this example:
In .NET Framework
httpClient = new HttpClient();
ServicePointManager.FindServicePoint(new Uri(_baseAddress)).ConnectionLeaseTimeout = 5 * 60 * 1000;
In .NET Core
var socketsHttpHandler = new SocketsHttpHandler()
{
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
};
httpClient = new HttpClient(socketsHttpHandler)
{
BaseAddress = new Uri(_baseAddress)
};
In detail, you can take a look at this article: https://makolyte.com/csharp-configuring-how-long-an-httpclient-connection-will-stay-open/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.