Search code examples
c#dynamics-crm.net-4.5tls1.2dynamics-365

Microsoft.CrmSdk.XrmTooling.CoreAssembly with Targetframework .NET 4.5.2 causes NullReference Exception on CrmServiceClient.Execute


We are connecting to Dynamics 365 CRM with the following code in a plain console app targeting .NET 4.5.2.

 private static void Main(string[] args)
    {
        RetrieveAllEntitiesRequest req2 = new RetrieveAllEntitiesRequest();
        req2.EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Entity;

        CrmServiceClient conn = new CrmServiceClient(
            "[email protected]",
            CrmServiceClient.MakeSecureString("secret"),
            "EMEA", "contoso",
            useUniqueInstance: true,
            useSsl: true, isOffice365: true);

        try
        {
            //error with target framework 4.5.2
            var resp2 = conn.Execute(req2) as RetrieveAllEntitiesResponse;
        }
        catch
        {
            string lastError = conn.LastCrmError;
        }
    }

The following nuget packages are in the packages.config

<package id="Microsoft.CrmSdk.CoreAssemblies" version="9.0.2.5" targetFramework="net452" />
<package id="Microsoft.CrmSdk.Deployment" version="9.0.2.4" targetFramework="net452" />
<package id="Microsoft.CrmSdk.Workflow" version="9.0.2.4" targetFramework="net452" />
<package id="Microsoft.CrmSdk.XrmTooling.CoreAssembly" version="9.0.2.7" targetFramework="net452" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.22.302111727" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />

When I run this code I get a NullReferenceException at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.Execute(OrganizationRequest request) with a LastCrmError of

Unable to Login to Dynamics CRM************ Exception - RetrieveAllEntities : Execute (RetrieveAllEntities) request to CRM from IOrganizationService |=> Object reference not set to an instance of an object.

As soon as I change the target Framework to 4.6.2 it works just fine. The thing is that we host this code inside an SQL Server Integration Services package and for some reason, on Windows 2016, SSIS always falls back to the 4.5.2 framework.

We are investigating this as well but for now the question remains, if the Microsoft.CrmSdk.XrmTooling.CoreAssembly package is supposed to work with 4.5.2 (the nuget readme says yes) or if this is a bug in the SDK.

The latest .NET 4.7.X frameworks have been installed on the 2016 server and my win 10 dev machine while both face the same issue.


Solution

  • Since version 9, Dynamics 365 is enforcing TLS 1.2 and because .NET 4.5 uses TLS 1.1 as default you're getting this error. In the previous article you'll find that the solution for client applications is the one you've already tried:

    Recompile your client applications using .NET framework 4.6.2 or higher.

    There're some articles on how to enforce TLS 1.2 from a SSIS package (here and here), but unfortunately I haven't tried them. If these don't help, you can workaround the error by updating the SchUseStrongCrypto registry setting to DWORD:00000001 as described here (Enable TLS 1.2 for dependent components/Update .NET Framework section).