gpt4 book ai didi

c# - 为什么在 RestClient.Execute 工作时将 RestClient.ExecuteAsync 与 await 一起使用会静默失败?

转载 作者:行者123 更新时间:2023-12-05 05:51:22 25 4
gpt4 key购买 nike

我目前正在与 Personio 集成以获取员工数据。

下面的解决方案不是等待响应,而是在代码中注释的地方突然跳出第二个方法:

using Personio.Client.Configuration;
using Personio.Client.Exceptions;
using Personio.Client.Models.Data;
using LanguageExt;
using Newtonsoft.Json;
using RestSharp;

namespace Enpal.Personio.Client;

public class PersonioCompanyEmployeesClient
{
private readonly PersonioAuthorizationClient _authorizationClient;
private readonly PersonioConfig _personioConfig;
private readonly RestClient _restClient;

public PersonioCompanyEmployeesClient(PersonioAuthorizationClient authorizationClient, PersonioConfig personioConfig, RestClient restClient)
{
_authorizationClient = authorizationClient;
_personioConfig = personioConfig;
_restClient = restClient;
}

public TryAsync<List<Record>> TryGet(EmployeeRequestOptions options) =>
_authorizationClient.WithAuthorization<List<Record>>(token =>
{
_restClient.BaseUrl = new Uri(_personioConfig.BaseUrl, "/v1/company/employees");
_restClient.Timeout = -1;

IRestRequest request = new RestRequest(Method.GET)
.AddQueryParameter("limit", options.MaximumRecordCount.ToString())
.AddQueryParameter("offset", options.PageOffset.ToString())
.AddHeader("Accept", "application/json")
.AddHeader("Authorization", $"Bearer {token.Value}");

return GetRecordsAsync(request);
});

private async Task<List<Record>> GetRecordsAsync(IRestRequest request)
{
IRestResponse<RecordRequestResponse> requestResponse = await _restClient.ExecuteAsync<RecordRequestResponse>(request);
// Nothing below this line is ever executed!
if (requestResponse.IsSuccessful)
{
RecordRequestResponse? employeesResponse = JsonConvert.DeserializeObject<RecordRequestResponse>(requestResponse.Content);
return (employeesResponse != null && employeesResponse.WasSuccessful)
? employeesResponse.Records
.ToList()
: throw new PersonioRequestException("Connected to Personio, but could not get records.");
}
else
{
throw (requestResponse.ErrorException != null)
? new PersonioRequestException("Could not get records from Personio.", requestResponse.ErrorException)
: new PersonioRequestException("Could not get records from Personio. Reason unknown.");
}
}
}

但是,我可以使用同步方法使这个解决方案工作,如下所示:

public TryAsync<List<Record>> TryGet(EmployeeRequestOptions options) =>
_authorizationClient.WithAuthorization<List<Record>>(token =>
{
_restClient.BaseUrl = new Uri(_personioConfig.BaseUrl, "/v1/company/employees");
_restClient.Timeout = -1;

IRestRequest request = new RestRequest(Method.GET)
.AddQueryParameter("limit", options.MaximumRecordCount.ToString())
.AddQueryParameter("offset", options.PageOffset.ToString())
.AddHeader("Accept", "application/json")
.AddHeader("Authorization", $"Bearer {token.Value}");

return GetRecordsAsync(request);
});

private async Task<List<Record>> GetRecordsAsync(IRestRequest request)
{
IRestResponse<RecordRequestResponse> requestResponse = await _restClient.ExecuteAsync<RecordRequestResponse>(request);
if (requestResponse.IsSuccessful)
{
RecordRequestResponse? employeesResponse = JsonConvert.DeserializeObject<RecordRequestResponse>(requestResponse.Content);
return (employeesResponse != null && employeesResponse.WasSuccessful)
? employeesResponse.Records
.ToList()
: throw new PersonioRequestException("Connected to Personio, but could not get records.");
}
else
{
throw (requestResponse.ErrorException != null)
? new PersonioRequestException("Could not get records from Personio.", requestResponse.ErrorException)
: new PersonioRequestException("Could not get records from Personio. Reason unknown.");
}
}

唯一的变化:

  1. GetRecords()使用 Execute而不是 ExecuteAsync
  2. GetRecords()返回 List<Record>而不是 Task<List<Record>>
  3. TryGet()包裹 GetRecordsAsync(request)Task.FromResult()在归还之前。

最佳答案

查看 restsharp 的文档后,ExecuteAsync 不会抛出异常,而是填充 response.ErrorExceptionresponse.ErrorMessage 如果response.IsSuccessful 等于 false,因此您应该首先检查 response.IsSuccessful 属性值。

可在此处找到更多详细信息 - https://restsharp.dev/intro.html#content-type

关于c# - 为什么在 RestClient.Execute 工作时将 RestClient.ExecuteAsync 与 await 一起使用会静默失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70382123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com