我遇到了使用我的代码从API获得响应的问题,请求没有超时,它根本没有给我回复.我已经在我自己的API中创建了一个api端点,以返回json字符串,然后用"Firefox Poster"手动发布json数据,它运行正常.有了这个,我相信问题出现在我的代码中.
我得到了一个C#WebAPI,我正在开发它与Angular前端一起使用(这是有效的,它仅用于历史).在调用我的API时,我创建了对象"EnrollmentRequestDto"
public class EnrollmentRequestDto
{
/// <summary>
/// Context Information for the request. Contains the Ship-To, language code and timezone.
/// </summary>
[JsonProperty("requestContext")]
public RequestContextDto RequestContext { get; set; }
/// <summary>
/// Unique ID provided by DEP to reseller on provisioning DEP access. This would be the reseller's DEP ID if its posted by distributor OBO a reseller, and would be his own depResellerId if a reseller is posting for self.
/// </summary>
[JsonProperty("depResellerId")]
public string DepResellerId { get; set; }
/// <summary>
/// Unique transaction ID provided by the reseller
/// </summary>
[JsonProperty("transactionId")]
public string TransactionId { get; set; }
/// <summary>
/// List of orders in the transaction (limit 1000 per transaction)
/// </summary>
[JsonProperty("orders")]
public List<OrderDto> Orders { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
创建此对象后,我发送到我的类RequestHandler和方法BulkEnrollRequest,其中atm是用HttpClient扩展名Flurl编写的,可以在这里找到:github
public IResponse BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
try
{
var result = _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>();
result.Wait();
return result.Result;
}
catch (FlurlHttpTimeoutException)
{
throw new AppleTimeOutException();
}
catch (FlurlHttpException ex)
{
return _errorHandler.DeserializeFlurlException(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个以确保在Flurl中没有任何反应(这只是为了调试到我想要获得响应的程度):
public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
var json = JsonConvert.SerializeObject(enrollmentRequest);
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient.PostAsync(_bulkEnrollUrl, new StringContent(json, Encoding.UTF8, "application/json"));
return new SuccessResponse();
}
Run Code Online (Sandbox Code Playgroud)
代码在等待点冻结,没有任何事情发生.我试图在BulkEnrollRequest之后放置一个断点,所以它永远不会离开这个方法.
现在这里是有趣的部分:它在我正在使用ErrorHandler时工作,并且在某些时候API刚刚停止响应.Wireshark显示正在提出请求......所以我被困住了.
任何帮助表示赞赏.
编辑
这现在有效!我从API控制器到RequestHandler一直实现异步,然后等待每次调用.以供参考:
public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
try
{
var result = await _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>();
return result;
}
catch (FlurlHttpTimeoutException)
{
throw new AppleTimeOutException();
}
catch (FlurlHttpException ex)
{
return _errorHandler.DeserializeFlurlException(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
这已经修复了.我相信Wireshark流量看起来如此奇怪的原因是由于代码中的死锁,并且超时在我身边,这意味着我永远无法确认信息.为了解决这个问题,我在所有方法上实现了异步,从Controller到我的RequestHandler类.以供参考:
public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest)
{
try
{
var result = await _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>();
return result;
}
catch (FlurlHttpTimeoutException)
{
throw new AppleTimeOutException();
}
catch (FlurlHttpException ex)
{
return _errorHandler.DeserializeFlurlException(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5156 次 |
| 最近记录: |