The underlying connection was closed: An unexpected error occurred on a send.

Using a web service to upload documents and update metadata in Sharepoint Online.  Actually need to update our existing web service which worked on Sharepoint 2013 and other version if they were on prem.

But now after uploading a few documents to sharepoint, we get the dreaded error.  Once this error happens, I have to reset IIS in order to continue.  I've tried adding this line of code: 

System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12

It takes a bit longer for it to occur than without that line, but it still happens.  Here's the full stack.


he underlying connection was closed: An unexpected error occurred on a send. - System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> 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.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.ConnectStream.WriteHeaders(Boolean async) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.Idcrl.SharePointOnlineAuthenticationProvider.GetIdcrlHeader(Uri url, Boolean alwaysThrowOnFailure, EventHandler`1 executingWebRequest) at Microsoft.SharePoint.Client.Idcrl.SharePointOnlineAuthenticationProvider.GetAuthenticationCookie(Uri url, String username, SecureString password, Boolean alwaysThrowOnFailure, EventHandler`1 executingWebRequest) at Microsoft.SharePoint.Client.SharePointOnlineCredentials.GetAuthenticationCookie(Uri url, Boolean refresh, Boolean alwaysThrowOnFailure) at Microsoft.SharePoint.Client.ClientRuntimeContext.SetupRequestCredential(ClientRuntimeContext context, HttpWebRequest request) at Microsoft.SharePoint.Client.SPWebRequestExecutor.GetRequestStream() at Microsoft.SharePoint.Client.ClientContext.GetWebRequestExecutor() at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate() at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() at Verbella.SPConnector.BusinessLogic.IntegrationLogic.GetAllLibraryNamesOfSite(String URL, String UserName, String Password, Boolean SSLEnabled, String CertPath) in C:\Visual Studio 2013\WebSites\Verbella.SPConnector\Verbella.SPConnector.BusinessLogic\IntegrationLogic.vb:line 221



Hi Paul,

Did you checked the TLS protocol version in the both sides: OutSystems and Sharepoint?


Solution

Leandro Correa wrote:

Hi Paul,

Did you checked the TLS protocol version in the both sides: OutSystems and Sharepoint?


I did on my side.  I can't check Sharepoint Online, and wouldn't be able to change if I could.

I also set my clientcontext.Request = Threading.Timeout.Infinite.  I had to upgrade to .NET 4.8 before that property was even available.

So far it seems to be working without any errors.  So in summary, it looks like setting:

ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12

as wells as the timeout to Infinite, seems to work.  I will be keeping my fingers crossed as I perform a stress test.  


Solution

Fingers crossed here too! :D