I recently tried to use the sample code in the Dynamics 365 SDK (also available here) to create a connection in code to the Dynamics CRM v9 Organisation service. No matter what I did, I could not get the sample code to work.
I was just getting a 'System.NullReferenceException' error, and the only other clue was that the connection object's LastCrmError property had a value of "Unable to Login to Dynamics CRM Organization Web Proxy Client is null:
After a significant amount of head-scratching, a colleague reminded me that Microsoft have recently announced that connections to Dynamics 365 v9 will only work if using TLS 1.2 or higher.
Once we’d established this was the problem, fixing the issue was quite easy: before creating the connection to CRM, you need to switch to using TLS 1.2, which can be done like this:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using the ServicePointManager in System.Net.
Until Microsoft update their sample code to include this, I hope that this saves someone the problems I had in getting the connection to work.
With thanks to Ian Cummings for helping me to resolve this issue!