Today I encountered a strange exception when working with CRM SDK v9 to build a console application connecting to a D365 CE instance. When I inspected the inner exception, it said:
An existing connection was forcibly closed by the remote host
It looks like the following screenshot
It is strange because what I was doing is just creating a organization service proxy, nothing much, and it’s working with other CRM versions. I googled it, someone said it was something relating to IIS server configuration, that wasn’t it because I’m working with Online instance. Then I found a change in connection security that it is using TLS 1.2 from version 9. More details here.
Okay, so what we need to do is using TLS 1.2 in the connection. As the post suggested, there is two ways to enable it with .Net code:
- Recompile your code with .Net framework 4.6.2.
- Utilize a registry setting on Windows that will force .NET to utilize the highest possible security standard.
I tried the first one but it didn’t work. Then I made sure the TLS 1.2 is enabled in the internet options and tried again but no luck.
I don’t want to update the registry. Luckily, there is a way to explicitly use the TLS 1.2 in your .Net code by adding the following line before any call to CRM service.
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
If you have problem downloading CRM SDK v9, there is a post may help here.
Hope it helps!