web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

What happened to my first WhoAmiIRequest code?

(0) ShareShare
ReportReport
Posted on by 137

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

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    the url format in your case should be

    testtoronto.crm3.dynamics.com

    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

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Please have a look below reference . Also you should use below line -

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    arunpotti.wordpress.com/.../comment-page-1

  • Verified answer
    Sreevalli Profile Picture
    3,256 on at

    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 :)

  • David Jennaway Profile Picture
    14,065 on at

    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

  • Peter Cong Profile Picture
    137 on at

    [quote user="Guido Preite"]

    the url format in your case should be

    testtoronto.crm3.dynamics.com

    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.

  • Peter Cong Profile Picture
    137 on at

    [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.

  • Peter Cong Profile Picture
    137 on at

    Actually, I am using .net framework 4.7.1 under Visual Studio 2015.

  • Peter Cong Profile Picture
    137 on at

    [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,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans