【发布时间】:2019-04-12 14:12:13
【问题描述】:
我需要使用 Windows 服务中的 Microsoft Graph 发送多封电子邮件。
我正在使用Microsoft.Graph NuGet 包。
我正在创建GraphServiceClient 并像这样发送邮件:
IGraphServiceClient graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", authenticationProvider);
var email = new Message
{
Body = new ItemBody
{
Content = "Works fine!",
ContentType = BodyType.Html,
},
Subject = "Test",
ToRecipients = recipientList
};
await graphClient.Users["test@example.onmicrosoft.com"].SendMail(email, true).Request().WithMaxRetry(5).PostAsync();
当我逐一发送电子邮件时:
for (var j = 0; j < 20; j++)
{
await graphClient.Users["test@example.onmicrosoft.com"].SendMail(email, true).Request().WithMaxRetry(5).PostAsync();
progressBar1.PerformStep();
}
一切正常,但是当我使用Parallel.For:
var res = Parallel.For(0, 20, async (i, state) =>
{
var email = new Message
{
Body = new ItemBody
{
Content = "Works fine!",
ContentType = BodyType.Html,
},
Subject = "Test",
ToRecipients = recipientList
};
await graphClient.Users["test@example.onmicrosoft.com"].SendMail(email, true).Request().WithMaxRetry(5).PostAsync();
});
我收到错误,因为我收到了太多请求 (429),然后是不支持的媒体类型 (415)。
这是错误代码:
代码:RequestBodyRead 消息:缺少或为空的内容类型标头 尝试阅读消息时发现。内容类型标头是 必填。
这是它在 Fiddler 中的样子:
我的问题是:我可以使用 Graph 以及如何使用 Parallel.For 来避免此类错误。我已经为每个请求设置了WithMaxRetry(5)。
我知道使用限制,但我认为WithMaxRetry(5) 会有所帮助。
【问题讨论】:
-
所有对 Microsoft Graph 的调用都应在开发时预期它们可能会受到限制。我建议您考虑在代码中加入 retry library。
-
@KalyanKrishna 我认为如果我添加
WithMaxRetry(github.com/microsoftgraph/msgraph-sdk-dotnet/blob/…) MS Graph SDK 将处理重试。看看这个 PR:github.com/microsoftgraph/msgraph-sdk-dotnet/pull/301
标签: c# .net microsoft-graph-api microsoft-graph-sdks microsoft-graph-mail