NullReferenceException is thrown from Microsoft.Graph library
Describe the bug I'm using the Microsoft.Graph library version 3.8 to make requests to Azure AD. However, sometime (quite rarely in fact), I am getting a NullReferenceException. I suppose that the exception is thrown because of some type of HTTP response, but cannot understand which response causes the exception. The code is following:
IUserRequestBuilder requestBuilder = this.adClient.Users[userPrincipalName];
List<Option> options = new List<Option>();
QueryOption option = new QueryOption("$select", "id,userPrincipalName,accountEnabled,usageLocation,onPremisesLastSyncDateTime,onPremisesImmutableId");
options.Add(option);
IUserRequest userRequest = requestBuilder.Request(options);
User user = await userRequest.GetAsync();
The exception call stack is the following:
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Graph.HttpProvider.<SendAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__35.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.BaseRequest.<SendAsync>d__31`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.UserRequest.<GetAsync>d__6.MoveNext()
To Reproduce Actually the exception is not reproduced stable.
Expected behavior The request should be either succeeded or a correct exception should be thrown.
Client version Microsoft.Graph 3.8 Microsoft.Graph.Core 1.20.1
Desktop (please complete the following information):
- OS: Windows 10
Hey @begemott , This could be probably due to a bug already fixed here.
To have the fix you would need to upgrade to at least version 1.23.0 of Microsoft.Graph.Core or upgrade to the latest version of the Microsoft.Graph(currently at 3.24.0) which should pull in the latest version of Microsoft.Graph.Core
Thanks, I'll try the latest version.
I had a similar issue. Was using Microsoft.Graph 1.21.0. I was trying to do
private async Task<DriveItem> CreateFolderIfNotExists(GraphServiceClient graphClient, string driveId, string folderName)
{
try
{
var driveItem = new DriveItem
{
Name = folderName,
Folder = new Folder(),
AdditionalData = new Dictionary<string, object>()
{
{ "@microsoft.graph.conflictBehavior", "fail" }
}
};
return await graphClient.Drives[driveId].Root.Children
.Request()
.AddAsync(driveItem);
}
catch (ServiceException exception)
{
if (exception.StatusCode != HttpStatusCode.Conflict)
{
throw;
}
return await this.GetFolderItem(graphClient, driveId, folderName);
}
}
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken) at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption) at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
Our resolution was to upgrade to latest version
Closing this as it it resolved now.