.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
相关产品推荐
相关产品推荐

