HI I have the following code, when I call the Ping method the first time, it works but on the second call it fails with already exists error
the Ping service is meant to keep alive for 16 seconds, once the timer counts to zero the user is dropped from the datatable, this way I have a list of currently connected users
public class PokerHost : WebService
{
//bool RunningTimmer = true;
static DataTable table = new DataTable();
private static readonly TimeSpan UpdateEngineTimerFrequency = TimeSpan.FromSeconds(2);
private Timer UpdateEngineTimer { get; set; }
private void MyTimerAction(object state)
{
DataTable table = GetTable(); // Get the data table.
foreach (DataRow row in table.Rows) // Loop over the rows.
{
int minused = Convert.ToInt32(row["countdown"]) - 2;
if (minused >= 0) {
row["countdown"] = minused;
}
else
{
row.Delete();
}
table.AcceptChanges();
}
}
static DataTable GetTable()
{
table.Columns.Add("gamekey", typeof(string));
table.Columns.Add("countdown", typeof(int));
return table;
}
protected void Application_Start(object sender, EventArgs e)
{
this.UpdateEngineTimer = new Timer(MyTimerAction,
null, /* or whatever state object you need to pass */
UpdateEngineTimerFrequency,
UpdateEngineTimerFrequency);
}
protected void Application_End(object sender, EventArgs e)
{
this.UpdateEngineTimer.Dispose();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
//--Only webmethods
//--------------------------------------------------------------------------------------------------------------------------------------------------
[WebMethod]
public string Ping(string gamekey)
{
DataTable table = GetTable(); // Get the data table.
foreach (DataRow row in table.Rows) // Loop over the rows.
{
if (Convert.ToString(row["gamekey"]) == gamekey)
{
row["countdown"] = 16;
}
table.AcceptChanges();
table.Dispose();
}
//make array of current online users
// need to check with the game and timelimits
table.AcceptChanges();
table.Dispose();
return "PONG";
}
As soon as the method is called the second time its killed , how do i fix this ,
Thanks for the time, this app is made and run in mono, on an ubuntu server
the error i am getting is this
500 - Internal Server Error
System.Data.DuplicateNameException: A DataColumn named 'gamekey' already belongs to this DataTable.
at System.Data.DataColumnCollection.RegisterName (System.String name, System.Data.DataColumn column) [0x00000] in <filename unknown>:0
at System.Data.DataColumnCollection.Add (System.Data.DataColumn column) [0x00000] in <filename unknown>:0
Your problem seems to be caused by attempting to set the same values to the variable
tableseveral times. You may be able to fix it by doing something like this:This is not an exact solution, and not tested in any way, but should hopefully give you an idea about what to try. The point is to only create and set values to
tableif it has not been done already (a simple singleton pattern).PS: If you want to / need to, you could also check if the table contains
"gamekey"and"countdown", instead of just checking if it isnull, as in the example.