简体   繁体   English

从多个线程(并行)使用GraphServiceClient-发送邮件

[英]use GraphServiceClient from multiple threads (parallel) - send mails

I have a need to send multiple emails using Microsoft Graph from Windows Service. 我需要使用Windows服务中的Microsoft Graph发送多封电子邮件。
I'm using Microsoft.Graph NuGet package. 我正在使用Microsoft.Graph NuGet包。
I'm creating GraphServiceClient and sending mail like so: 我正在创建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();

When I send emails one-by-one: 当我一一发送电子邮件时:

for (var j = 0; j < 20; j++)
{
    await graphClient.Users["test@example.onmicrosoft.com"].SendMail(email, true).Request().WithMaxRetry(5).PostAsync();
    progressBar1.PerformStep();
}

everything works fine, but when I use Parallel.For : 一切正常,但是当我使用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();
});

i get errors, because I get Too Many Requests (429) and then Unsupported Media Type (415). 我收到错误消息,因为我收到太多请求(429),然后收到不支持的媒体类型(415)。

This is the error code: 这是错误代码:

Code: RequestBodyRead Message: A missing or empty content type header was found when trying to read a message. 代码:RequestBodyRead消息:尝试读取消息时,发现内容类型标头丢失或为空。 The content type header is required. 内容类型标头是必需的。

This is how it looks in Fiddler: 这是在Fiddler中的外观:

在此处输入图片说明

My question is: can I use and how I should use Graph with Parallel.For to avoid this kind of errors. 我的问题是:我可以使用Graph和Parallel.For来避免这种错误。 I'm already setting WithMaxRetry(5) for each request. 我已经为每个请求设置了WithMaxRetry(5)

I'm aware of usage limits, but I thought WithMaxRetry(5) will help. 我知道使用限制,但是我认为WithMaxRetry(5)会有所帮助。

It doesn't have to do with the threads. 它与线程无关。 It has to do with throtteling aka you can only do x amount of requests in a certain time period. 它与节流有关,也就是您在特定时间段内只能执行x个请求。

The dotnet graph api client doesn't support batching (sadly enough). dotnet graph api客户端不支持批处理(足够糟糕)。 But batching those requests yourself is easily implemented. 但是,您可以轻松实现批量处理这些请求。 Then you can send 15 mails with one request. 然后,您可以通过一个请求发送15封邮件。

The reason why you saw this is because of the missing content type header. 之所以看到此原因,是因为缺少内容类型标头。 It wasn't being cloned when we cloned the httprequestmessage. 当我们克隆httprequestmessage时,它没有被克隆。 This has been fixed and will be in the next release of the client. 此问题已修复 ,将在客户端的下一版本中发布。 With regards to parallel threads, we plan on implementing a resource based shared retry queue so that we use a single retry scheme across multiple requests that target the same resource (and the same throttling policy). 关于并行线程,我们计划实现一个基于资源的共享重试队列,以便我们在针对相同资源(和相同限制策略)的多个请求中使用单个重试方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM