I have a code that loops continuously and during each iterations an item is added to SharePoint list CSOM. The code runs fine for some time and then I get an error
“The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ”
Any idea how to resolve this issue. The code snippet is given below
ClientContext context = <<get context refrence>>;
List announcementsList = context.Web.Lists.GetByTitle(“CSOM”);
for (int i = 0; i < 1000000; i++)
{
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem[“Title”] = DateTime.Now;
newItem.Update();
context.ExecuteQuery();
}
Hi Nikhil,
Have you tried to set “RequestTimeout” property in ClientContext?.
Is it working when set Request Time Out?
Try running this in your session. I have it in my powershell profile to make sure it’s always set but you could also add it to your script or just your session.
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;