I'm using a windows service for call API, get response and update in Sql Table It's working fine but some time it's Hit API Twice. I'm unable to got reason behind. Here is my code
protected override void OnStart(string[] args)
{
this.timer = new System.Timers.Timer(15000D);
this.timer.AutoReset = true;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
this.timer.Start();
}
protected override void OnStop()
{
this.timer.Stop();
this.timer = null;
}
protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.proccessQue();
}
And here is proccessQue() method
//SELECT record form table
SqlDataAdapter adap = new SqlDataAdapter("SELECT * FROM TABLE_NAME WHERE is_done=0 AND date>DATEADD(minute,-5,GETDATE())", conn);
DataTable dt = new DataTable();
adap.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
string parameters= dt.Rows[i]["parameters"] + "";
string api = "http://domain.com/page.aspx?parameters=" + parameters;
HttpWebRequest httpreq = (HttpWebRequest)WebRequest.Create(api);
HttpWebResponse httpres = (HttpWebResponse)httpreq.GetResponse();
StreamReader sr = new StreamReader(httpres.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
if (results.Contains("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"))
{
try
{
string response= "";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(results);
var res2 = xmlDoc.SelectNodes("RechargeRequest/RequestResponse/APIRef");
if (res2 != null)
response= res2[0].InnerText;
SqlCommand cmd = new SqlCommand("UPDATE TABLE_NAME SET field='" + response+ "',is_done=1 WHERE id=" + rId, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
}
}
}
Please help me where is I am going wrong.