一年后,应用程序现在抛出:System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭

After a year, app now throws : System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

我一直在四处寻找,但无法找到为什么这个问题突然冒出来的答案。 我有一个 4.5 .NET C# 控制台应用程序 运行 一年多没问题,但现在抛出以下错误:

发生异常:System.Net.WebException:基础连接已关闭:接收时发生意外错误。 ---> System.IO.IOException: 无法从传输连接读取数据: 现有连接被远程主机强行关闭。 ---> System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭 在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead) --- 内部异常堆栈跟踪结束 ---

   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)

试图执行 ClientContext.ExecuteQuery() 时抛出异常。应用程序上的任何内容都没有改变。我正在等待 SP 服务器团队的响应,看看他们这边是否有任何变化,但是这里有人知道是什么导致了这个新问题吗?

顺便说一句,我正在使用 SharePoint Foundation REST。

提前致谢。

如果您使用的是 .NET 4.5,您的默认 TLS 协议是 TLSv1。我打赌你的服务器停止支持它,这就是你得到异常的原因。

解决方案:

  • 升级到较新的 .NET 版本(4.7.2 或更高版本)。

  • 启用 TLSv12 协议:

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    

更多信息:here and