web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

System.IO.IOException: Unable to read data from the transport connection

(0) ShareShare
ReportReport
Posted on by 1,577

Hello, 

I have a custom-code import application which writes incoming new records to CRM or updates if the record already exists. The first thing the import does is query the ContactSet using the IOrganisationService to create a list of existing contacts (a scaled-down custom class (called “ContactData”) holding just the crmid and emailaddress). This list is then used to compare incoming records to see if they already exist as Contacts in CRM. 

using (var context = new XrmServiceContext(service))
               {
                 
  contactdataList = (from a in context.CreateQuery("contact")    
       select new ContactData

{
             CrmId = (Guid)a["contactid"],

      Email = (string)a["emailaddress1"]

}

}

This has been working fine every night for months without any errors or changes until last night when the error below was logged when executing the code above.

exception 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.GZipWrapperStream.Read(Byte[] buffer, Int32 offset, Int32 size)

   at System.ServiceModel.Channels.PreReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)

   at System.ServiceModel.Channels.HttpInput.WebResponseHttpInput.WebResponseInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)


NOTE: If I reduce the contact query by filtering on e.g. just one email address, the import runs successfully. 

Does anyone know what could be causing this error? Have I hit some kind of threshold for this type of query where the number of contacts has surpassed? 

There are 337K Contacts in total. 

Any suggestions gratefully received. 

Thanks,

- Seamus

I have the same question (0)
  • Wahaj Rashid Profile Picture
    11,321 on at
  • Suggested answer
    prt33k Profile Picture
    6,907 on at

    Enable TLS - write this before making SDK call on top of the function:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

    Another suggestion related will be to use Upsert instead of retrieval of 337K contacts.

    carldesouza.com/.../

  • Seamus Profile Picture
    1,577 on at

    Thank you - I tried that but it made no difference. If I reduce the contact query by filtering on e.g. just one email address, the import runs successfully so that suggests it is not a TLS issue but a capacity-handling issue?

  • prt33k Profile Picture
    6,907 on at

    Usually, we get a timeout error for the capacity issues. Are you seeing any timeout too?

    Also, how are you importing the record - ExecuteMultiple for just a for loop?

    It would be helpful to see the request making part of the code.

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    As mentioned by Prateek, it would be great if you can share connection/request part of your code as well.

    Furthermore, to confirm if it is related to number of records, user .Take() to select top 100k records (for example) and verify if it works.

    Here you can see examples on Take:

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/org-service/sample-more-linq-query-examples

    If it is related to number of records, I would suggest to use Paging (as mentioned before).

  • Seamus Profile Picture
    1,577 on at

    Thanks for all you help.
    I do not get a timeout.. I took both your advice and added ".Take()" to my query before creating the list. I added this in increments of 100k to see if the error occured after a certain number of records. The strange thing is, when I use .Take() it works, even when I specify a greater number of records than we actually have....yet before I used .Take() it was throwing this error. I don't see how it can handle a specific number rather than "select all", especially when the specified number is larger than the number of records which would be returned by "select all".

  • Seamus Profile Picture
    1,577 on at

    Thanks everyone for your help - apparently this was something Microsoft did (re pagination) and resolved within a week.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans