问题描述
大家好,
我编写了以下代码(将从旧版应用程序以及.NET dll中调用该代码):
I've written the following code (which is going to be called from the legacy application as well as from .NET dll):
/// <param name="tnTimeout">The number of seconds to wait for the HTTP POST call to complete and return a response</param>
/// <param name="tnRetries">If no respone in the timeout period, the number of times to attempt contact the host.
/// If it was a DNS lookup failure or other glaring connection issue, then retries can be bypassed..</param>
/// <param name="tnReturnValue">Passed by reference, it is the "ReturnValue" string in the JSON response</param>
/// <param name="tcReturnValueText">Passed by reference, it is the "ReturnValueText" string in the JSON response</param>
/// <param name="tcMessage">"Message" string in the JSON response, if present</param>
/// <param name="tcProductCode">"ExternalProductCode" string in the JSON response, if present</param>
/// <returns>int Status</returns>
/// </summary>
public int CheckPass(String tcHost, String tcUserName, String tcPassword, String tcBarcode,
String tcUsageDate, Int32 tnTimeout, Int32 tnRetries,
ref Int32 tnReturnValue, ref String tcReturnValueText, ref String tcMessage, ref String tcProductCode)
{
Int32 statusCode = 1;
try
{
SetRequestHeaders(tcHost, tcUserName, tcPassword, tnTimeout);
var json = new specificJSON();
var ticket = new TicketUsageRequest(tcBarcode, tcUsageDate, tcUserName);
json.TicketUsageRequests.Add(ticket);
Task<TicketUsageResponse> task = GetResponse(json, "path here");
TicketUsageResponse result = task.Result;
tcMessage = result.ReturnData[0].Message;
tcReturnValueText = result.ReturnValueText;
tnReturnValue = result.ReturnValue;
tcProductCode = result.ReturnData[0].ExternalProductCode;
}
catch (Exception e)
{
tcReturnValueText = e.Message;
statusCode = 0;
}
return statusCode;
}
async Task<TicketUsageResponse> GetResponse(specificJSON json, String url)
{
TicketUsageResponse ticketUsageResponse = null;
using (HttpResponseMessage response = await client.PostAsJsonAsync(url, json))
{
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
ticketUsageResponse = await content.ReadAsAsync<TicketUsageResponse>();
}
}
else
{
Int32 statusCode = (Int32) response.StatusCode;
throw new HttpException(statusCode, response.ReasonPhrase);
}
}
return ticketUsageResponse;
}
我想以某种方式将尝试次数引入此代码中(请参阅tnTries参数).我们应该检查是否存在超时,如果超时,请重试.
I want to somehow introduce number of tries into this code (see tnTries parameter). We should check if there is a timeout and if so, re-try.
我也找到了此讨论内容, http://stackoverflow .com/questions/10547895/how-can-i-tell-when-httpclient-has-timed-out 但我不确定 如何将其合并到我的代码中.
I also found this discussion http://stackoverflow.com/questions/10547895/how-can-i-tell-when-httpclient-has-timed-out but I am not exactly sure how to incorporate it into my code.
我也很欣赏有关该代码的建设性评论-我该如何改进它.
And I also appreciate a constructive comments about that code - how can I improve it.
谢谢.
对于每个专家,都有一个平等的和相反的专家. -贝克尔定律
我的博客
我的TechNet文章
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
推荐答案
在我的一个项目中,我使用了Polly来管理重拨和紧急呼叫的相关延迟.
Within a project of mine I used Polly to manage retry and related delay for my critical calls.
https://github.com/App-vNext/Polly
https://github.com/App-vNext/Polly
这篇关于HttpClient-介绍尝试次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!