'Graph query works in console app, fails in azure function
I am querying Azure Active Directory using Microsoft.Graph. My method (shown below) works in a console app i created as a POC. When i try and use the method in an azure function it returns a HTTP 500 error, but does not give me any information to track down the issue.
The issue occurs on the below line:
IGraphServiceUsersCollectionPage usersPage = await graphClient
.Users
.Request()
.Filter($"Department eq 'Information Technology'")
.Select("Department,DisplayName,Mail,Id")
.GetAsync();
The program hangs there for about 5 seconds and then postman shows a 500 internal error. Appreciate advice on how to troubleshoot or resolve this.
Full Method:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string tenantId = "xxxx-xxxx-xxxx-xxxx";
string clientId = "xxxx-xxxx-xxxx-xxxx";
string clientSecretValue = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
List<User> users = new List<User>();
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecretValue);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
IGraphServiceUsersCollectionPage usersPage = await graphClient
.Users
.Request()
.Filter($"Department eq 'Information Technology'")
.Select("Department,DisplayName,Mail,Id")
.GetAsync();
users.AddRange(usersPage.CurrentPage);
return new OkObjectResult(users);
}
** I'm aware hard coding secrets is terrible practice, this was just a proof of concept before investing too much time.
UPDATE: The function is long running (taking about 3 minutes to run completely). It seems it is running correctly if i get its URL and execute it in a browser, it just does not run correctly in the "Code & Test feature" on the azure portal, functions blade
Solution 1:[1]
You can run the azure function from Azure protal's function blade and check the error in logs window. Read more details at this Microsoft documentation.
Regarding failure, best guess can be that function itself failing to start due to missing configuration setting like AzureWebJobStorage
Solution 2:[2]
Could be something related to dll's version. For .netcore 3.1 adding this line under .csproj file fixed the issue.. or upgrade to .Net6.0 (VS2022)
<PropertyGroup>
...
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user1672994 |
| Solution 2 | san |

