0

I have created a console application that connects to CRM and updates a field in an XYZ entity.

The code to connect to CRM is -

ClientCredentials credential = new ClientCredentials();
credential.UserName.UserName =user;
credential.UserName.Password = password;

OrganizationServiceProxy proxy = new OrganizationServiceProxy(new Uri(crmEnvironments.ElementAt(currentEnvironment).URL), null, credential, null);

It connects to CRM well initially but after sometime it throws the below mentioned error -

"Metadata contains a reference which could not be resolved".

Inner Exception : "Unable to connect to Remote Server"

After doing some research on google , i did the following the things

Inserted the following line of code in Connect CRM -

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain,SslPolicyErrors sslPolicyErrors) { return true; };

Inserted the following in the Appconfig file -

<system.net>
 <defaultProxy useDefaultCredentials="true">
 </defaultProxy>
 </system.net>

However the error still exists, after processing some records the connection fails and throws the above mentioned error.

halfer
  • 19,824
  • 17
  • 99
  • 186
MVC_Nhibernate
  • 447
  • 11
  • 30

3 Answers3

1

What kind of deployment do you have? Is it OnPremise using Active Directory or IFD?

Just saying because you only need the ServerCertificateValidationCallback if you have CRM hosted over SSL with a Self-Signed certificate that you are actually bypassing by using the above method.

If it processes some records and then stops, then I bet the ServerCertificateValidationCallback has nothing to do with that. It might be CRM dropping the connection because you exceeded the timeout, for example.

You could increase the OrganisationService timeouts in the CRM's web.config.

See more details here

Another better option, if possible, would be to wrap your individual calls into a ExecuteMultipleRequest so that you are not making N calls to the service, just one big one (it will save network bandwidth and connection issues).

Community
  • 1
  • 1
Jordi
  • 1,460
  • 9
  • 9
  • Thank you for your reply. I am working on CRM 2015 Online. if you dont mind can you suggest me an example where ExecuteMultipleRequest is used. – MVC_Nhibernate Apr 25 '16 at 13:56
0

Another way to connect a console app is CrmServiceClient, which is in the Microsoft.Xrm.Tooling.Connector library.

Using NuGet you can get the Microsoft.CrmSdk.XrmTooling.CoreAssembly package and you'll be off to the races.

Here are a couple other helpful links:

Build Windows client applications using the XRM tools

Sample: Quick start for XRM Tooling API

Here are various connection string examples:

CRM 2016 and Dynamics 365 online:

<add name="dev26" connectionString="Url=https://dev26.crm.dynamics.com; Username=user@dev26.onmicrosoft.com; Password=Pass; AuthType=Office365" />

On-premise with integrated security:

<add name="prod" connectionString="Url=http://myserver/AdventureWorksCycle;"/>

On-premise with credentials:

<add name="prod" connectionString="Url=http://myserver/AdventureWorksCycle; Domain=mydomain; Username=administrator; Password=password; AuthType=AD;"/>

On-premise IFD before CRM 2016:

<add name="prod" connectionString="Url=https://contoso.litware.com; Username=someone@litware.com; Password=password; AuthType=IFD;"/>

On-premise IFD for CRM 2016 and later (v8.0+)

<add name="prod" connectionString="ServiceUri=https://contoso.litware.com/contoso; Domain=contoso; Username=contoso\administrator; Password=password; AuthType=IFD; LoginPrompt=Never;" />
Aron
  • 3,877
  • 3
  • 14
  • 21
0

I know its been a few years since this question was asked; but I just ran into this issue today trying to connect to CRM 365 Online.

I kept getting a null object error when trying to connect - it turned out the root cause was I had not configured my proxy credentials in my app.config file.

Malachy
  • 307
  • 8
  • 16