Running MS Graph SDK functionalities as BackgroundService does not work

APIPointNewbie 146 Reputation points
2021-11-23T15:41:12.447+00:00

Hello dear community,

I would like to run the delta queries of the MS Graph SDK in a background worker (ASP .Net Core).

public class Background : BackgroundService, IHostedService
{
   //......
  private async Task<IDriveItemDeltaCollectionPage> GetFilesAsync(GraphServiceClient graphClient, object deltaLink)
    {
        IDriveItemDeltaCollectionPage page;

        if (Delta.lastPage == null)
        {
            var query = new List<QueryOption>() {
            new QueryOption("$select", "name")
            };

            page = await graphClient.Drives[DriveId]
                                    .Items[ItemId]
                                    .Delta()
                                    .Request(query)
                                    .GetAsync();
        }
        else
        {
            lastPage.InitializeNextPageRequest(graphClient, deltaLink.ToString());
            page = await Delta.lastPage.NextPageRequest.GetAsync();
        }
        lastPage = page;

        return page;
    }
 }

But unfortunately I get the following exception:
'Microsoft.Graph.ServiceException' occurred in System.Private.CoreLib.dll

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,781 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,059 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,304 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 71,511 Reputation points
    2021-11-23T16:20:05.643+00:00

    no reason it shouldn't work. what is the error?

    are you using the desktop authentication examples? as it is a background service, there is no request to get credentials from.


  2. Hervin De La Cruz Betancourt 1 Reputation point
    2021-11-23T20:51:54.77+00:00

    Sometimes it takes a couple of minutes for the permissions to propagate, and this creates a problem. If you have problems, validate first of all the permissions of your application in the applications registry click on grant
    https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-access-web-apis


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.