Dynamics 365: The underlying connection was closed

Problem

While upgrading a .NET customisation that interfaced with our client Dynamics 365 online environment (Version 9). I encountered the following error while attempting to fetch some records from our Dynamics 365 instance.

The same code had previously worked without issue in our Dynamics 365 Version 8 environment.

“Metadata contains a reference that cannot be resolved: ‘https://XXXX.api.crm6.dynamics.com/XRMServices/2011/Organization.svc?wsdl&sdkversion=9’.”

{“The underlying connection was closed: An unexpected error occurred on a send.”}

{“Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.”}

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

Error Screenshot

Solution

As per the below Microsoft article Dynamics 365 now requires TLS 1.2

https://support.microsoft.com/en-au/help/4051700

 

The solution is to either:

  • Use .NET framework version 4.6 which uses TLS 1.2 by default

or

  • Add the below line of code to your app (before making calls to Dynamics 365) to explicitly enable TLS 1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

 

Shane Bartholomeusz

Shane Bartholomeusz

G’day, I’m a technical solutions consultant based in Perth, Australia that specialises in the Microsoft technology stack, including .NET, Dynamics 365, Azure and general web development etc.

This blog is my place to share my thoughts, helpful solutions and just random nerdy stuff.
Shane Bartholomeusz

Latest posts by Shane Bartholomeusz (see all)

Leave a Reply