Reece Cottam Reece Cottam - 1 month ago 17
ASP.NET (C#) Question

Why is my WCF Service timing out when I try to fill my datalist?

I have an MVC app wired up to a wcf service, I'm trying to make a todo list app that features tasks that are dependant on other tasks, I have so far had no luck with this but I have built a stored proc, which I'm now calling from my WCF service and giving a response back to the client.

At the moment, the client gives the following error upon being non-responsive for 1 minute;

"The request channel timed out while waiting for a reply after 00:01:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout."


the code on my WCF service looks like this;

public DataTable GetAllDependantTasks(string id)
{
DataTable dt = new DataTable();
try
{
using (SqlConnection conn = new SqlConnection())
using (SqlCommand com = new SqlCommand())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ToDoDatabase"].ConnectionString;
com.Connection = conn;
com.CommandText = "usp_GetAllDependantTaskInfoByID";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("@id", id));
conn.Open();
SqlDataReader returnvalue = com.ExecuteReader();

if (returnvalue.HasRows)
{
//List<DataRow> items = new List<DataRow>();
dt.Load(returnvalue);

// foreach (DataRow row in dt.Rows)
// {
// items.Add(row);
//}

return dt;
}
else
{
throw new RowNotInTableException();
}

}
}
catch (Exception ex)
{
//TODO: Write error to log
return new DataTable();
}
}


I am using an ASPX page on my front end, the offending code looks like this;

private void LoadTasks()
{
// get the todo list items
ToDoService.ToDoServiceClient client = new ToDoService.ToDoServiceClient();

try
{
// List<ToDoService.ToDoItemContract> toDoItems = client.GetToDoItems("").ToList();


List<string> Items = new List<string>();

foreach(var row in client.GetAllDependantTasks("").Rows)
{
Items.Add(row.ToString());
}

dlTasks.DataSource = Items;


dlTasks.DataBind();

client.Close();
}
catch (Exception ex)
{
// TODO: Log error
client.Abort();
}
}


Could anyone point me in the right direction with this? I would like to fill my datalist on the front end with the existing tasks plus the task they are dependant upon, i've tried to return a datatable from my wcf service to bind it to the list but it doesn't seem to like it!

thanks in advance :)

Answer

Maybe the data returned is a lot and it takes time. SqlCommand has a 15 seconds default timeout.

What if you try to do

com.Connection = conn;
com.CommandTimeout = 0;

This disable a timeout for connection and allows more time for the execution of the fill. Try to adjust it (the time is in millisecond) to not leave it running indefinitely if something goes wrong.

If not, try setting the operation timout of the webservice with

Webservice.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(10);

Setting it to a longer timespan since the default wcf timespan is 1:00 which is what is happening to you.

Comments