英文:
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 = "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();
}
答案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'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) =>
{
Console.WriteLine($"Request returned status code {httpResponse.StatusCode}");
// 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($"Request failed, let's retry after a delay of {delayInSeconds} seconds");
else if (attempt == MaxRetry)
Console.WriteLine($"This was the last retry attempt {attempt}");
else
Console.WriteLine($"This was retry attempt {attempt}, let's retry after a delay of {delayInSeconds} seconds");
return true;
}
};
var requestOptions = new List<IRequestOption>
{
retryHandlerOption,
};
User? user = await graphClient
.Users[userId]
.GetAsync(requestConfiguration => requestConfiguration.Options = requestOptions);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论