Forum Discussion

mbalcarek's avatar
mbalcarek
Copper Contributor
Oct 07, 2021

Intermittent - An existing connection was forcibly closed by the remote host - ExecuteQuery()

Hello,

I have run the following code against our SharePoint Online, from several servers and local PCs and I get an intermittent error. It works on a server for a while, and then fails on the same server for a while:

 

IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

 

Is this my issue or an issue with SharePoint Online?

Using .net framework 4.7.2, nuget Microsoft.SharePointOnline.CSOM package 16.1.21312.12000

 

 

 

         static void Main(string[] args)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
.
.
           string webUrl = "https://tenant.sharepoint.com/sites/SandBox";
           using (Microsoft.SharePoint.Client.ClientContext ctx =
                new Microsoft.SharePoint.Client.ClientContext(webUrl))
            {
                SecureString securePassword = new NetworkCredential("", "pass").SecurePassword;
                ctx.Credentials = new SharePointOnlineCredentials("user@tenant.onmicrosoft.com", securePassword);
                Web web = ctx.Web;
                ctx.Load(web, w => w.Title, w => w.Description);
                ctx.ExecuteQuery();
            }

 

 

 

 Unhandled Exception: 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.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
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.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.TlsStream.CallProcessAuthentication(Object state)
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 executingWeb
Request)
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 PCS.ConsoleApp.SPPermissions.Program.Main(String[] args) 

2 Replies

  • rorysullivan's avatar
    rorysullivan
    Copper Contributor

    Hi mbalcarek,

    We experienced a very similar issue recently. We are using the Exchange Online Webservice instead of SharePoint, but otherwise the issue was identical; intermittently getting the "connection forcibly closed' exception.

    We solved the issue by updating the .NET Framework version that our app was targeting. In our case going from .NET Framework 4.5.1 to .NET Framework 4.8.

    As of yet we have not established exactly why this works but our support technicians believe it is down to the TLS version that is being used to communicate with the server. .NET Framework 4.7 introduces some updates to how TLS is handled that we believe solves the issue.

     

  • Sigar_Dave's avatar
    Sigar_Dave
    Copper Contributor
    It looks like an error due to a failure in internet connection to me. As you said the same code works for you sometimes and then failed. Look for the Settings in your system

Resources