Search code examples
c#dynamics-crm

CrmServiceClient Error: 2 : Unable to connect to CRM: Method not found: OrganizationDetail.get_EnvironmentId()


Trying to switch two existing applications from using OAuth and a GUI User to an Application User, I'm facing the problem that connecting to CRM using Microsoft.Xrm.Tooling.Connector.CrmServiceClient fails when deployed to the target machine while it works fine running in Visual Studio.

The error I receive, tracing CrmServiceClient is:

Microsoft.Xrm.Tooling.Connector.CrmServiceClient Error: 2 : Unable to connect to CRM: Method not found: 'System.String Microsoft.Xrm.Sdk.Organization.OrganizationDetail.get_EnvironmentId()'.

up until this point, authentication itself seems to work just fine:

Microsoft.Xrm.Tooling.Connector.CrmServiceClient Verbose: 16 : Initialize CRM connection Started - AuthType: ClientSecret
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Verbose: 16 : Direct Login Process Started
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Information: 8 : Attempting to Connect to Uri https://xxx.crm.dynamics.com/XRMServices/2011/Organization.svc
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Verbose: 16 : DiscoveryServer indicated organization service location = https://xxx.crm.dynamics.com/XRMServices/2011/Organization.svc
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Information: 8 : Organization Service URI is = https://xxx.crm.dynamics.com/XRMServices/2011/Organization.svc
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Verbose: 16 : ConnectAndInitCrmOrgService - Initializing Organization Service Object
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Information: 8 : ConnectAndInitCrmOrgService - Requesting connection to Organization with CRM Version: No organization data available
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Information: 8 : AuthenticateService - found authority with name https://login.microsoftonline.com/xxx/oauth2/authorize
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Information: 8 : AuthenticateService - found resource with name https://xxx.crm.dynamics.com/
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Verbose: 16 : ObtainAccessToken - Client Secret
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Verbose: 16 : Added WebClient Header Hooks to the Request object.
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Information: 8 : ConnectAndInitCrmOrgService - Proxy created, total elapsed time: 00:00:00.7981767

Code to reproduce the issue

using System;
using System.Net;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Tooling.Connector;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            string cs = "AuthType=ClientSecret;Url=https://xxx.crm.dynamics.com;ClientId=00000000-0000-0000-0000-000000000000;ClientSecret=my-secret-here";
            CrmServiceClient client = new CrmServiceClient(cs);

            if (!client.IsReady)
            {
                throw new Exception(client.LastCrmError);
            }

            var service = client.OrganizationWebProxyClient;

            var whoamirequest = new WhoAmIRequest();

            var whoamiresponse = service.Execute(whoamirequest);
        }
    }
}

Referenced packages are

Microsoft.CrmSdk.CoreAssemblies 9.0.2.25
Microsoft.CrmSdk.Deployment 9.0.2.25
Microsoft.CrmSdk.Workflow 9.0.2.25
Microsoft.CrmSdk.XrmTooling.CoreAssembly 9.1.0.42
Microsoft.IdentityModel.Clients.ActiveDirectory 3.19.8
Microsoft.Rest.ClientRuntime 2.3.20
Newtonsoft.Json 10.0.3

I tried changing the target framework version from 4.6.2 to 4.8 and tried several versions of the related packages.

The only obvious difference between the machine I build and debug on and the machine I ran the app on is dev machine having the developer package of .NET Framework 4.6.2 and 4.8 and the other machine having the client profile .NET Framework.


Solution

  • The issue is caused by an earlier version (9.0.9002.0) of Microsoft.Xrm.Sdk.dll installed in the GAC of the target machine.

    This assembly contains the portion of code Microsoft.Xrm.Sdk.Organization.OrganizationDetail.get_EnvironmentId() showing up in the trace log.

    Using gacutil /l Microsoft.Xrm.Sdk, you are able to identify, which versions are deployed in the GAC.
    In my case it was an earlier version deployed for use by other services running on this machine.

    Since removing the offending version from the GAC was not a viable option in this case, I simply imported the new version (9.0.46.3545) of Microsoft.Xrm.Sdk into the GAC.