簡體   English   中英

圖查詢在控制台應用程序中有效,在 azure function 中失敗

[英]Graph query works in console app, fails in azure function

我正在使用 Microsoft.Graph 查詢 Azure Active Directory。 我的方法(如下所示)適用於我作為 POC 創建的控制台應用程序。 當我嘗試在 azure function 中使用該方法時,它返回 HTTP 500 錯誤,但沒有給我任何信息來追蹤問題。

問題出現在以下行:

IGraphServiceUsersCollectionPage usersPage = await graphClient
    .Users
    .Request()
    .Filter($"Department eq 'Information Technology'")
    .Select("Department,DisplayName,Mail,Id")
    .GetAsync();

該程序在那里掛起大約 5 秒鍾,然后 postman 顯示 500 內部錯誤。 感謝有關如何排除故障或解決此問題的建議。

完整方法:

[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);
}

** 我知道硬編碼秘密是一種糟糕的做法,這只是在投入太多時間之前的概念證明。

更新:function 運行時間很長(大約需要 3 分鍾才能完全運行)。 如果我得到它的 URL 並在瀏覽器中執行它,它似乎運行正確,它只是在 azure 門戶的“代碼和測試功能”中運行不正確,功能刀片

在此處輸入圖像描述

您可以從 Azure protal 的 function 刀片運行 azure function 並檢查日志 window 中的錯誤。閱讀Microsoft 文檔中的更多詳細信息。

關於失敗,最好的猜測可能是 function 本身由於缺少AzureWebJobStorage等配置設置而無法啟動

可能與 dll 的版本有關。 對於 .netcore 3.1,在 .csproj 文件下添加此行解決了問題。或升級到 .Net6.0 (VS2022)

 <PropertyGroup>
        ...
        <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
 </PropertyGroup>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM