0

I have been searching around but can't find an answer to why this problem is popping up out of nowhere. I have a 4.5 .NET C# console application that has been running fine for over a year, but is now throwing the following error:

Exception Occurred: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead) --- End of inner exception stack trace ---

   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
   at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at Sharepoint_Audit_Automation.SharepointAudit.returnUsersfromView(String listName, String viewName, ClientContext clientContext, WebClient client, XNamespace d, String recipientField)
   at Sharepoint_Audit_Automation.SharepointAudit.Main(String[] args)

The exception is being thrown when it tries to execute ClientContext.ExecuteQuery(). Nothing on the application has changed. I am waiting for the SP server team response to see if anything has changed on their side, but does anyone here have any idea what could have caused this new problem?

I am working with SharePoint foundation REST by the way.

Thanks in advance.

Alex
  • 3
  • 3
  • We saw a similar error with one of our apps when some firewall rules were changed and again when the server was changed from a single physical machine with a static IP to multiple servers with virtual IPs. You may be able to do more error handling in code if the problem is rare and intermittent. If not, the fix may not be in your code. – DSway Aug 11 '20 at 17:24
  • Take a look at this. https://stackoverflow.com/questions/4015324/how-to-make-an-http-post-web-request It related to creating multiple instances of HttpWebRequest. – hocho Aug 11 '20 at 17:42

1 Answers1

3

If you are using .NET 4.5, your default TLS protocol is TLSv1. I bet your server stopped supporting it and that's the reason your are getting the exception.

Solution:

  • Upgrade to the newer .NET version (4.7.2 or higher).

  • Enable the TLSv12 protocol:

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    

More info: here and here

EylM
  • 5,967
  • 2
  • 16
  • 28
  • The SP team never got back to me on exactly what it was, but it was about a .NET version upgrade, so I am pretty sure this was the problem. Thanks for the advice! – Alex Aug 18 '20 at 00:14