5

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){}

2 Answers2

3

On the Networking of the Breaking 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, and WebClient.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
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • 4
    It just says to use the HttpClient class instead, but nothing more than that. What else needs to happen? – NovaDev Nov 08 '22 at 22:54
  • @NovaDev for what? – Paulo Morgado Nov 09 '22 at 08:23
  • They say use the HttpClient, but they don't say how exactly. For example, there was something like this before: servicePoint.UseNagleAlgorithm = false; What is the alternative to that in HttpClient? – Max Nov 12 '22 at 20:01
  • What changed from 5.0 to 6.0? The source code for all versions is available at https://github.com/dotnet/runtime/. You just need to select the tag you want. – Paulo Morgado Nov 14 '22 at 08:36
0

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/

Tang Thanh Tam
  • 431
  • 3
  • 12