This is my first coding of CRM, I am trying to connect my Microsoft Dynamic 365 for Customer Engagement trial version.
from here: https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/org-service/quick-start-org-service-console-app
static void Main(string[] args)
{
string url = "testtoronto.crm3.dynamics.com/main.aspx";
string userName = "mike@testtoronto.onmicrosoft.com";
string password = "test1234$";
string conn = $@"
Url = {url};
AuthType = Office365;
UserName = {userName};
Password = {password};
RequireNewInstance = True";
using (var svc = new CrmServiceClient(conn))
{
WhoAmIRequest request = new WhoAmIRequest();
WhoAmIResponse response = (WhoAmIResponse)svc.Execute(request); // here is not compiled, "object variable not defined"
Console.WriteLine("Your UserId is {0}", response.UserId);
Console.WriteLine("Press anykey to exit.");
Console.ReadLine();
}
}
Sorry, this is my first CRM coding, Can anybody help me to fix it? much appreciated!
*This post is locked for comments
[quote user="Goutam Das"]
Hi ,
Please have a look below reference . Also you should use below line -
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
arunpotti.wordpress.com/.../comment-page-1
[/quote]
after added this line code:
xxxxxxxxxxxxx
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // just added this line code before service call, it works great
using (var svc = new CrmServiceClient(conn))
{
xxxxxxxxxxxxxx
it works great, thank you so much,
Actually, I am using .net framework 4.7.1 under Visual Studio 2015.
[quote user="David Jennaway"]
Check the LastCrmError or LastCrmException property of the CrmServiceClient - this should give you information about why the connection failed. If you get the error 'An existing connection was forcibly closed by the remote host', then this indicates that you need to use TLS1.2, as per the suggestions above
[/quote]
This is the error log of LastCrmError:
Unable to Login to Dynamics CRM
Unable to Login to Dynamics CRM************ NullReferenceException - WhoAmI : Execute (WhoAmI) request to CRM from IOrganizationService |=> Object reference not set to an instance of an object.
Object reference not set to an instance of an object.[TerminalFailure] Failed to Execute Command - WhoAmI : RequestID=b751109c-c482-49f7-987a-7ee37bf6a90a : Execute (WhoAmI) request to CRM from IOrganizationService duration=00:00:00.0052088 ExceptionMessage = Object reference not set to an instance of an object.
Object reference not set to an instance of an object.
[quote user="Guido Preite"]
the url format in your case should be
I know it's a trial, but if you posted your real password, please edit the message and remove it
make sure also that there are no syntax errors in the conn variable
[/quote]
I changed the url as you mentioned, but unfortunately, I got the same error.
The error is still on this line: WhoAmIResponse response = (WhoAmIResponse)svc.Execute(request); //Object reference not set to an instance of an object.
Check the LastCrmError or LastCrmException property of the CrmServiceClient - this should give you information about why the connection failed. If you get the error 'An existing connection was forcibly closed by the remote host', then this indicates that you need to use TLS1.2, as per the suggestions above
Hi,
If you are using .net framework 4.5 or below you should enforce the security protocol type to TLS 1.2. so you should add below code line before calling service.
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
As a side note, avoid sharing urls, creddentails. always its good to mask :)
Hi ,
Please have a look below reference . Also you should use below line -
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
the url format in your case should be
I know it's a trial, but if you posted your real password, please edit the message and remove it
make sure also that there are no syntax errors in the conn variable
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6