We have a Windows Service written in .NET that we use to "sync" data into SharePoint Online. What the service does is to update items in a particular SharePoint list every 30 minutes (configurable, but doesn't seem to be related to the problem.)

After the service has been running for a while without issue, sometimes 2 weeks, sometimes more, it fails to connect to SPO and from then on will fail every time it runs with the same error (the IP address of the SPO URL may change in the error but everything else is the same.)

When someone notices that the data in the list is stale, we restart the service and it works perfectly. Obviously we can institute workarounds such as emailing a server admin when we run into this error, or have a watchdog restart our service, but we would really prefer to fix the root cause.

The exception that we get is:

14/09/2017 00:00:06.61  Error      SharePoint Online           Validation            (STGMModelStatusSettings) An error occurred accessing SharePoint Online and/or associated services. Exception: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.DoPost(String url, String contentType, String body, Func`2 webExceptionHandler)
  at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetUserRealm(String login)
   at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetServiceToken(String username, String password, String serviceTarget, String servicePolicy)
   at Microsoft.SharePoint.Client.Idcrl.SharePointOnlineAuthenticationProvider.GetAuthenticationCookie(Uri url, String username, SecureString password, Boolean alwaysThrowOnFailure)
   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.GetFormDigestInfoPrivate()
   at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at WorkbenchService.SharePointOnline.SPODataSync.ValidateConfigurationForSTGMTasks(ClientContext& clientContext, Web& web, List& list) in C:\redacted\SPODataSync.cs:line 133

Line 133 of the service is as follows: enter image description here

My first thought was some kind of leak but the clientContext is disposed correctly even on exception and is newed up every time so I'm not sure that can be it.

  • I am facing the similar issue, Service not able to connect to SharePoint online after a random time. some time a day , some time three or four days.after restart of app pool it works as expected. – vinit kumar May 2 '18 at 9:04
  • coxy, did you find any solution for this issue – vinit kumar May 2 '18 at 10:10
  • @vinitkumar nope, we never fixed it. The workaround we implemented was to start a new AppDomain every time that does the actual connection to SharePoint. Even that fails sometimes but when it fails the next attempt is in a fresh AppDomain and succeeds. – Coxy May 2 '18 at 12:33

I have fixed this issue for all time. not sure if it help for others too. In My case i added a proxy into the web config of the Asp.net service which was calling the SharePoint online to download the documents from the Library.

> <system.net>
>     <defaultProxy useDefaultCredentials="true">
>       <proxy proxyaddress="proxyAddress" usesystemdefault="True"/>
>     </defaultProxy>   </system.net>

After providing the proxy id we never did restart of App pool and works perfect.

Hope helps somebody.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.