【发布时间】:2018-02-06 19:53:15
【问题描述】:
请随意创建一个 Windows 窗体应用程序。要重现错误,请禁用网络连接并运行代码。它每 1 秒尝试重新连接一次。在 4-5 次尝试启用网络连接后,在调试模式下,您会注意到 Reconnect() 方法被调用了 4-5 次,即使产品已被获取。获取产品后为什么要一次又一次地调用 Reconnect() 方法?
string apiUrl = "https://api.gdax.com/products";
string json;
private void Form1_Load(object sender, EventArgs e)
{
try
{
if (FillProducts()) // product need first
{
}
}
catch (WebException ex)
{
ReconnectOnError(ex);
}
}
private bool FillProducts()
{
bool isDone = false;
try
{
json = GetGDXJSONData(apiUrl);
JsonSerializer serializer = new JsonSerializer();
DataTable dt = (System.Data.DataTable)Newtonsoft.Json.JsonConvert.DeserializeObject(json, (typeof(System.Data.DataTable)));
count = dt.Rows.Count;
if (count > 0)
isDone = true;
}
catch (Exception ex)
{
isDone = false;
ReconnectOnError(ex);
}
return isDone;
}
int count = 0;
private void ReconnectOnError(Exception errorMessage)
{
try
{
Thread.Sleep(1000);
if (count < 1)
{
FillProducts(); // it comes on this point again and again even the count is greater than 1
Reconnect();
}
else
{
Reconnect();
}
}
catch (WebException ex)
{
ReconnectOnError(ex);
}
}
private void Reconnect()
{
// why this is called the number of times the attempt was made to fill the products?
}
private string GetGDXJSONData(string apiUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "GET";
request.ContentType = "application/json";
request.UserAgent = "gdax-node-client";
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
}
编辑 - 这是一个示例代码,一旦获取产品,我将在 Reconnect() 方法中做很多事情。 此外,如果在获取产品后发生错误,则不要获取产品,只需调用 Reconnect() 方法,这就是 else 的原因。
Edit2 - 请不要仅仅通过查看代码来回复。如果您创建了一个表单并自己运行它并且可以自己看到错误,那么请告知如何解决此问题。
更新 - 我知道我进入了无限迭代。我试过了,现在可以了:
string apiUrl = "https://api.gdax.com/products";
string json;
private void Form1_Load(object sender, EventArgs e)
{
if (FillProducts()) // product need first
{
}
}
bool isOk = false;
private string GetGDAXProducts()
{
try
{
json = GetGDXJSONData(apiUrl);
return json;
}
catch (Exception ex)
{
return "-1";
}
}
int count = 0;
private bool FillProducts()
{
bool isDone = false;
string retVal = GetGDAXProducts();
while (retVal == "-1")
{
retVal = GetGDAXProducts();
}
if (retVal != "-1")
{
JsonSerializer serializer = new JsonSerializer();
DataTable dt = (System.Data.DataTable)Newtonsoft.Json.JsonConvert.DeserializeObject(json, (typeof(System.Data.DataTable)));
count = dt.Rows.Count;
if (count > 0)
isDone = true;
}
return isDone;
}
private string GetGDXJSONData(string apiUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "GET";
request.ContentType = "application/json";
request.UserAgent = "gdax-node-client";
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
【问题讨论】:
-
这是由于
if (count < 0)中的其他部分 -
你在fillproducts之后再调用它...
-
我必须这样做。但它并没有出现在那条线上。如果你运行代码,你会看到它调用了 FillProducts();重新连接();一次又一次,即在没有互联网时第一次尝试重新连接的次数
-
你有一个间接递归调用——
FillProducts()调用ReconnectOnError(ex);如果有一个异常调用FillProducts()。 -
请在没有互联网的情况下运行代码并调试它,然后在尝试重新连接 4-5 次后启动互联网。请不要停止应用程序