You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8中如何为GraphAPI调用添加ConsistencyLevel: Eventual

解决Graph API调用中缺少ConsistencyLevel:eventual请求头的问题

正确的代码实现

针对你的场景,在Microsoft Graph SDK 4.x版本中添加ConsistencyLevel:eventual请求头的代码如下:

@inject GraphServiceClient GSC
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler

// ...

protected async Task AzTest()
{
    try
    {
         var users = await GSC.Users.Request()
               .Header("ConsistencyLevel", "eventual")
               .Filter("endswith(mail, 'mydomain.com')")
               .GetAsync();
    }
    catch(Exception ex)
    {
         ConsentHandler.HandleException(ex);
    }
}

错误原因及解决说明

  • 关于Operator 'endsWith' is not supported because the 'ConsistencyLevel:eventual' header is missing错误:使用endswith这类模糊匹配运算符查询用户时,Graph API强制要求添加ConsistencyLevel:eventual请求头,用于保证目录查询的最终一致性。通过.Header()方法即可为请求添加该头信息。
  • 关于'IGraphServiceUsersCollectionRequestBuilder' does not contain a definition for 'GetAsync'编译错误:GSC.Users返回的是请求构建器对象,必须先调用.Request()方法获取可执行的请求对象,才能调用GetAsync()等执行方法。
  • 关于CS1660: Cannot convert lambda expression to type 'CancellationToken' because it is not a delegate type错误:GetAsync()方法的参数是可选的CancellationToken,不能传入lambda表达式或其他不匹配的类型,直接调用无参的GetAsync()即可。

当前你使用的NuGet包版本(Microsoft.Graph 4.36.0)完全支持上述写法,无需升级包即可解决问题。

内容的提问来源于stack exchange,提问作者M.M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 21:13:08