GraphServiceClient 并获取额外的数据

GraphServiceClient and get additional data

我使用 GraphServiceClient 从 Outlook 获取消息。

var messages = await _graphServiceClient.Me.MailFolders[folderId].Messages.Request()
    .Header("Prefer", "outlook.body-content-type=\"text\"")
    .Filter($"createdDateTime gt {greaterDate} and createdDateTime lt {lessDate}")
    .Top(int.MaxValue).GetAsync();

请求最多获得 1000 个项目。

我想收到其他消息并关注documentation

我的代码:

var pageIterator = PageIterator<Message>
        .CreatePageIterator(
            _graphServiceClient,
            messages,
            // Callback executed for each item in
            // the collection
            (m) =>
            {
                return true;
            },
            // Used to configure subsequent page
            // requests
            (req) =>
            {
                // Re-add the header to subsequent requests
                req.Header("Prefer", "outlook.body-content-type=\"text\"");
                return req;
            });

await pageIterator.IterateAsync();

但是当我调用我的代码时没有任何反应。看起来 pageIterator.IterateAsync(); 之后是无限循环。

怎么了?

我发现了 Top 查询列表消息的限制。以下说法来自list message api.

Depending on the page size and mailbox data, getting messages from a mailbox can incur multiple requests. The default page size is 10 messages. Use $top to customize the page size, within the range of 1 and 1000.

来自 OData 的描述 Top parameter

The minimum value of $top is 1 and the maximum depends on the corresponding API.

============================================= ====

.Top(int.MaxValue) 恐怕你最好将值设置为 999,因为我不确定它是否有最大值限制。而且我也在我这边测试了迭代器,它确实花了很多时间,所以我添加了一个 count 就像文档中显示的那样。

public async Task<string> mailsAsync() {
            //var mail = await _graphServiceClient.Me.Messages.Request().GetAsync();
            int count = 0;
            var messages = await _graphServiceClient.Me.Messages
                                .Request()
                                .Header("Prefer", "outlook.body-content-type=\"text\"")
                                .Select(e => new {
                                    e.Sender,
                                    e.Subject,
                                    e.Body
                                })
                                .Top(300)
                                .GetAsync();

            var pageIterator = PageIterator<Message>
                .CreatePageIterator(
                    _graphServiceClient,
                    messages,
                    (m) =>
                    {
                        Console.WriteLine(m.Subject);
                        count++;
                        return true;
                    },
                    // Used to configure subsequent page
                    // requests
                    (req) =>
                    {
                        // Re-add the header to subsequent requests
                        req.Header("Prefer", "outlook.body-content-type=\"text\"");
                        return req;
                    }
                );

            await pageIterator.IterateAsync();
            count = count + 1;
            return "success";
        }