在dotnet图形SDK v5中,WithMaxRetry和WithShouldRetry在哪里

huangapple go评论68阅读模式
英文:

Where is WithMaxRetry and WithShouldRetry in dotnet graph SDK v5

问题

在 Microsoft Graph SDK(dotnet)的第4版本中,您可以使用流畅的方法 .WithShouldRetry.WithMaxRetry。请参见此 博客文章

现在我正在将我的应用程序升级到SDK的第5版本,因为删除了 .Request(),所以不再可以设置重试。

在SDK v5中,我们如何实现重试?

更新:
请参阅SDK第5版本的新 博客文章

英文:

In version 4 of the Microsoft Graph SDK (dotnet) you had the ability to use the fluent methods .WithShouldRetry and .WithMaxRetry. See in example this blogpost.

I'm now upgrading my application to v5 of the SDK and because of the removal of .Request() it's not possible to set the retry anymore.

How can we implement the retry in SDK v5?

Update:
See new blogpost for v5 of the SDK.

答案1

得分: 2

retryhandler现在是Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options中的IRequestOption,所以你可以像这样做:

using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;

private static Event CreateEvent(GraphServiceClient graphServiceClient, string emailAddress)
{
    var requestBody = new Event
    {
        Subject = "Let's go for lunch",
        Body = new ItemBody
        {
            ContentType = BodyType.Html,
            Content = "Does mid month work for you?",
        },
        Start = new DateTimeTimeZone
        {
            DateTime = "2023-03-15T12:00:00",
            TimeZone = "Pacific Standard Time",
        },
        End = new DateTimeTimeZone
        {
            DateTime = "2023-03-15T14:00:00",
            TimeZone = "Pacific Standard Time",
        },
        Location = new Location
        {
            DisplayName = "Harry's Bar",
        },
        TransactionId = "7E163156-7762-4BEB-A1C6-729EA81755A7",
    };
    RetryHandlerOption retryHandlerOption = new RetryHandlerOption()
    {
        MaxRetry = 5
    };
    var requestOptions = new List<IRequestOption>
    {
        retryHandlerOption,
    };
    return graphServiceClient.Users[emailAddress].Calendar.Events.PostAsync(requestBody, rc => rc.Options = requestOptions).GetAwaiter().GetResult();
}
英文:

The retryhandler is now a IRequestOption in Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options so you can do something like

    using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;

    private static Event CreateEvent(GraphServiceClient graphServiceClient, string emailAddress)
    {
        var requestBody = new Event
        {
            Subject = &quot;Let&#39;s go for lunch&quot;,
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = &quot;Does mid month work for you?&quot;,
            },
            Start = new DateTimeTimeZone
            {
                DateTime = &quot;2023-03-15T12:00:00&quot;,
                TimeZone = &quot;Pacific Standard Time&quot;,
            },
            End = new DateTimeTimeZone
            {
                DateTime = &quot;2023-03-15T14:00:00&quot;,
                TimeZone = &quot;Pacific Standard Time&quot;,
            },
            Location = new Location
            {
                DisplayName = &quot;Harry&#39;s Bar&quot;,
            },
            TransactionId = &quot;7E163156-7762-4BEB-A1C6-729EA81755A7&quot;,
        };
        RetryHandlerOption retryHandlerOption = new RetryHandlerOption()
        {
            MaxRetry = 5
        };           
        var requestOptions = new List&lt;IRequestOption&gt;
        {                
            retryHandlerOption,
        };
        return graphServiceClient.Users[emailAddress].Calendar.Events.PostAsync(requestBody, rc =&gt; rc.Options = requestOptions).GetAwaiter().GetResult();
    }

答案2

得分: 1

// 使用随机的 Guid 作为不存在的用户以强制重试
string userId = Guid.NewGuid().ToString();

const int MaxRetry = 5; // 调用次数为 MaxRetry + 1(1 为原始调用)

RetryHandlerOption retryHandlerOption = new RetryHandlerOption()
{
    MaxRetry = MaxRetry,
    ShouldRetry = (delay, attempt, httpResponse) =>
    {
        Console.WriteLine($"请求返回状态码 {httpResponse.StatusCode}");

        // 在此处添加更多状态码或更改 if 语句…
        if (httpResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            return false;

        double delayInSeconds = CalculateDelay(httpResponse, attempt, delay);

        if (attempt == 0)
            Console.WriteLine($"请求失败,让我们在 {delayInSeconds} 秒后重试");
        else if (attempt == MaxRetry)
            Console.WriteLine($"这是最后一次重试尝试 {attempt}");
        else
            Console.WriteLine($"这是重试尝试 {attempt},让我们在 {delayInSeconds} 秒后重试");

        return true;
    }
};

var requestOptions = new List<IRequestOption>
{
    retryHandlerOption,
};

User? user = await graphClient
    .Users[userId]
    .GetAsync(requestConfiguration => requestConfiguration.Options = requestOptions);
英文:

@Glen Scales pointed me in the right direction. Here a more simple example with custom retry functionality.

// using Microsoft.Kiota.Abstractions;
// using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;

// Use random Guid for user that doesn&#39;t exist to force retry
string userId = Guid.NewGuid().ToString();

const int MaxRetry = 5; // So number of call are MaxRetry + 1 (1 is the original call)

RetryHandlerOption retryHandlerOption = new RetryHandlerOption()
{
    MaxRetry = MaxRetry,
    ShouldRetry = (delay, attempt, httpResponse) =&gt;
    {
        Console.WriteLine($&quot;Request returned status code {httpResponse.StatusCode}&quot;);

        // Add more status codes here or change your if statement...
        if (httpResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            return false;

        double delayInSeconds = CalculateDelay(httpResponse, attempt, delay);

        if (attempt == 0)
            Console.WriteLine($&quot;Request failed, let&#39;s retry after a delay of {delayInSeconds} seconds&quot;);
        else if (attempt == MaxRetry)
            Console.WriteLine($&quot;This was the last retry attempt {attempt}&quot;);
        else
            Console.WriteLine($&quot;This was retry attempt {attempt}, let&#39;s retry after a delay of {delayInSeconds} seconds&quot;);

        return true;
    }
};

var requestOptions = new List&lt;IRequestOption&gt;
{
    retryHandlerOption,
};

User? user = await graphClient
    .Users[userId]
    .GetAsync(requestConfiguration =&gt; requestConfiguration.Options = requestOptions);

huangapple
  • 本文由 发表于 2023年3月9日 22:19:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定