Home > OS >  Microsoft Graph throwing NullReferenceException while Subscribing to Calendar notifications
Microsoft Graph throwing NullReferenceException while Subscribing to Calendar notifications

Time:08-21

I am encountering a weird problem with Microsoft Graph as this piece of code has been working fine for years in Production but it has recently started to break.

Microsoft Graph returns a NullReferenceException when I try to subscribe to a user's Calendar notifications.

I have tested this on an outlook.com email so far. I am using .NET Framework 4.8 and Microsoft.Graph v1.21.0.0.

Here's my code:

        public async Task<string> SubscribeToWebhookNotifications(string NewCalendarName)
    {
        var Client = await GetClientAsync();

        NewCalendarName = NewCalendarName ?? ExternalAppConfig.MICROSOFT_DEFAULT_CALENDAR;


        if (string.IsNullOrWhiteSpace(await GetCalendarID(NewCalendarName)))
        {
            throw new BusinessException("SubscribeToWebhookNotification", "MicrosoftApp", string.Format("Invalid calendar name provided. Calendar Name: {0}", NewCalendarName), Resource.Manage_SaveSyncConsent_Invalid_Calendar);
        }

        var subscription = new Microsoft.Graph.Subscription()
        {
            ChangeType = ExternalAppConfig.MICROSOFT_WEBHOOK_CHANGE_TYPES, // created, updated, deleted
            NotificationUrl = ExternalAppConfig.MICROSOFT_WEBHOOK_NOTIFICATION_URL,
            Resource = ExternalAppConfig.MICROSOFT_WEBHOOK_EVENT_RESOURCE,
            ExpirationDateTime = DateTime.Now.AddMinutes(4230),
            ClientState = ExternalAppConfig.MICROSOFT_WEBHOOK_SECRET_VALUE,
        };

        var data = await Client.Subscriptions
                .Request()
                .AddAsync(subscription); // This is the piece of code that throws a NullReferenceException 

        return data.Id;
    }

CodePudding user response:

I don't think that MS Graph throws a NullReferenceException. My guess is that the request result is null (type is Subscription?) and you are trying to access Id of null. Because you didn't change your code and it worked, I would check if the variables from ExternalAppConfig are valid or loaded correctly.

  1. Do the variables from ExternalAppConfig are loaded correctly (non empty, non null)?
  2. Has the url of your backend or your webhook validation route changed? This would be relevant for MICROSOFT_WEBHOOK_NOTIFICATION_URL.
  3. Is the user that you are using still active (assuming you are not using the /me endpoint)? Relevant for MICROSOFT_WEBHOOK_EVENT_RESOURCE.

But the probability that this is the issue is quite low because then you would normally get an ServiceException (f.e. http status code 404 if user or calendar was not found).


What I just noticed is that you didn't post the message of the exception. So it could also be possible that Client or ExternalAppConfig is null.

  • Related