Skip to main content
Question

Basic C# example not working.

  • 17 February 2022
  • 9 replies
  • 471 views

Hello, first time user here. I'm trying to test the most basic code sample for C# but it isn't working and it's throwing a very vague exception. Code and exception below. I replaced the sensitive variables with comments describing what they had been set to.

using System;
using System.Threading.Tasks;
using RingCentral;
using DotNetEnv;

namespace Call_Ringout
{
class Program
{
static RestClient restClient;
static void Main(string[] args)
{
restClient = new RestClient(
/*Client ID*/,
/*Client Secret*/,
"https://platform.devtest.ringcentral.com");
restClient.Authorize(
/*Username*/,
/*Extension*/,
/*Password*/;
call_ringout().Wait();
}

static private async Task call_ringout()
{
var parameters = new MakeRingOutRequest();
parameters.from = new MakeRingOutCallerInfoRequestFrom
{
phoneNumber = /*From Number*/
};
parameters.to = new MakeRingOutCallerInfoRequestTo
{
phoneNumber = /*To Number*/
};
parameters.callerId = new MakeRingOutCallerInfoRequestTo
{
phoneNumber = /From Number/
};
parameters.country = new MakeRingOutCoutryInfo
{
id = "1"
};
parameters.playPrompt = false;
//var resp = await restClient.Restapi().Account().Extension().RingOut().Post(parameters);
//Console.WriteLine("Call Placed. Call status" + resp.status.callStatus);

var _1 = restClient.Restapi();
var _2 = _1.Account();
var _3 = _2.Extension();
var _4 = _3.RingOut();
try
{
var resp = await _4.Post(parameters);
Console.WriteLine("Call Placed. Call status" + resp.status.callStatus);
}
catch(Exception e)
{
Console.WriteLine(e.InnerException);
Console.WriteLine(e.Message);
Console.WriteLine(e.ToString());
}
}
}
}

I broke up the line

var resp = await restClient.Restapi().Account().Extension().RingOut().Post(parameters);

Into its component parts to see where the problem is. It is in the last part, which is why I put the try block around it alone.

Here is the error I'm getting:

Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at RingCentral.RestClient.Request(HttpRequestMessage httpRequestMessage, Int32 retriedTimes, Nullable`1 cancellationToken)
at RingCentral.RestClient.Request(HttpMethod httpMethod, String endpoint, Object content, Object queryParams, Nullable`1 cancellationToken)
at RingCentral.RestClient.Request[t](HttpMethod httpMethod, String endpoint, Object content, Object queryParams, Nullable`1 cancellationToken)
at RingCentral.RestClient.Post[t](String endpoint, Object content, Object queryParams, Nullable`1 cancellationToken)
at RingCentral.Paths.Restapi.Account.Extension.RingOut.Index.Post(MakeRingOutRequest makeRingOutRequest, Nullable`1 cancellationToken)
at Call_Ringout.Program.call_ringout() in C:Userswscheffeysource
eposCall_RingoutCall_RingoutProgram.cs:line 55

C:Program Filesdotnetdotnet.exe (process 28572) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Any help greatly appreciated.

I am having the same issue when doing a calllog, has anybody found a solution to this ?

1711633870057.png


I don't see how you create the query params so I cannot say what could be wrong. But get the latest .NET SDK and try this code.

using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
using RingCentral;
using Newtonsoft.Json
namespace Read_User_CallLog
{
class Program
{
static RestClient restClient;
static void Main(string[] args)
{
try
{
DotEnv.Load();
// Instantiate the SDK
restClient = new RestClient(
"RC_CLIENT_ID",
"RC_CLIENT_SECRET",
"RC_SERVER_URL");

// Authenticate a user using a personal JWT token
await restClient.Authorize( "RC_JWT" );

await read_user_calllog();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/*
* Read user call log between a period of time
*/
static private async Task read_user_calllog()
{
try
{
var queryParams = new ReadUserCallLogParameters();
queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
queryParams.dateTo = "2024-01-31T23:59:59.009Z";
queryParams.view = "Detailed";

var resp = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);
foreach (CallLogRecord record in resp.records)
{
Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
}
}
catch (Exception ex)
{
Console.WriteLine("Cannot read user call log data. " + ex.Message);
}
}
}
}

using Newtonsoft.Json;
using RingCentral;
using System;
using System.Threading.Tasks;

namespace Read_User_CallLog
{
class Program
{
static RestClient restClient;
static async Task Main(string[] args)
{
try
{
// Instantiate the SDK
RestClient restClient = new RestClient(
"<myclientid>",
"<mysecret>",
"https://dummy.com"
);
// Authenticate a user using a personal JWT token
// I have to use ConfigureAwait instead of wait cause it will
//just never return with wait.

restClient.Authorize("<MyJWT>").ConfigureAwait(false);
await read_user_calllog(
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

static private async Task read_user_calllog()
{
try
{
var queryParams = new ReadUserCallLogParameters();
queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
queryParams.dateTo = "2024-01-31T23:59:59.009Z";
queryParams.view = "Detailed";

var resp = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);

foreach (CallLogRecord record in resp.records)
{
Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
}
}
catch (Exception ex)
{
Console.WriteLine("Cannot read user call log data. " + ex.Message);
}
}
}
}

1711663637580.png

Thank you for replying. My code was in a .net API. I changed it to a console app but still getting the same issue, any help would be welcome.


You declared the static RestClient restClient in your class, then you declare a local restClient????

// Instantiate the SDK
RestClient restClient = new RestClient(
"<myclientid>",
"<mysecret>",
"https://dummy.com"
);

Remove it!

// Instantiate the SDK
restClient = new RestClient(
"<myclientid>",
"<mysecret>",
"https://dummy.com"
);

I still get the same error message

1711686390458.png


class Program
{
static RestClient restClient = new RestClient(
"<myclientid>",
"<mysecret>",
"https://dummy.com"
);
static async Task Main(string[] args)
{
try
{
// Authenticate a user using a personal JWT token
restClient.Authorize("<MyJWT>").ConfigureAwait(false);

await read_user_calllog();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/*
* Read user call log between a period of time
*/
static private async Task read_user_calllog()
{
try
{
var queryParams = new ReadUserCallLogParameters();
queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
queryParams.dateTo = "2024-01-31T23:59:59.009Z";
queryParams.view = "Detailed";

var resp = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);
foreach (CallLogRecord record in resp.records)
{
Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
}
}
catch (Exception ex)
{
Console.WriteLine("Cannot read user call log data. " + ex.Message);
}
}

It's not the same error. Before, the restClient was null.

What is the reason you call the authorize this way?

restClient.Authorize("<MyJWT>").ConfigureAwait(false);

Try this to see if it helps

await restClient.Authorize("<MyJWT>"));

I changed everything to be await and I am now able to get records back, however it is only returning calls I have made. I am an admin on our account. Please help me with this.


 class Program
{

static RestClient restClient = new RestClient(
"<myclientid>",
"<mysecret>", true
);
static async Task Main(string[] args)
{
try
{
// Authenticate a user using a personal JWT token
await restClient.Authorize("<MyJWT>");

var ringCallLogResponse = await read_user_calllog();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

static private async Task<CallLogResponse> read_user_calllog()
{
CallLogResponse callLog = new CallLogResponse();
try
{
var queryParams = new ReadUserCallLogParameters();
queryParams.dateFrom = "2024-01-01T00:00:00.000Z";
queryParams.dateTo = "2024-01-31T23:59:59.009Z";
queryParams.view = "Detailed";

callLog = await restClient.Restapi().Account().Extension().CallLog().List(queryParams);

foreach (CallLogRecord record in callLog.records)
{
Console.WriteLine(JsonConvert.SerializeObject(record, Formatting.Indented));
}
}
catch (Exception ex)
{
Console.WriteLine("Cannot read user call log data. " + ex.Message);
}

return callLog;

}
}

When I run it at https://developers.ringcentral.com/api-reference/Call-Log/readCompanyCallLog. I get back 99 pages with 100 records per page.


I removed the .Extension() and changed ReadUserCallLogParameters to ReadCompanyCallLogParameters and it works. Thank you for you help.


Reply