Skip to main content

Notifications

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

  • Peter Cong Profile Picture
    137 on at
    RE: What happened to my first WhoAmiIRequest code?

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

  • Peter Cong Profile Picture
    137 on at
    RE: What happened to my first WhoAmiIRequest code?

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

  • Peter Cong Profile Picture
    137 on at
    RE: What happened to my first WhoAmiIRequest code?

    [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
    RE: What happened to my first WhoAmiIRequest code?

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

  • David Jennaway Profile Picture
    14,065 on at
    RE: What happened to my first WhoAmiIRequest code?

    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

  • Verified answer
    Sreevalli Profile Picture
    3,256 on at
    RE: What happened to my first WhoAmiIRequest code?

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

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: What happened to my first WhoAmiIRequest code?

    Hi ,

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

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

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

  • Suggested answer
    Guido Preite Profile Picture
    54,077 Moderator on at
    RE: What happened to my first WhoAmiIRequest code?

    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

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans