当我尝试填充数据列表时,为什么我的WCF服务会超时?

时间:2016-10-09 20:07:08

标签: c# asp.net wcf datatable

我有一个连接到wcf服务的MVC应用程序,我正在尝试创建一个todo列表应用程序,其中包含依赖于其他任务的任务,到目前为止我没有运气,但我已经构建了一个存储proc,我现在从我的WCF服务中调用并向客户端发回响应。

目前,客户端在1分钟内没有响应时会出现以下错误;

"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."

我的WCF服务上的代码如下所示;

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();
            }
        }

我在前端使用ASPX页面,违规代码看起来像这样;

 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();
            }
        }

有人能指出我正确的方向吗?我想在前端填充我的datalist与现有任务加上他们所依赖的任务,我试图从我的wcf服务返回一个数据表,将它绑定到列表但是它似乎不喜欢它!

提前感谢:)

1 个答案:

答案 0 :(得分:0)

也许返回的数据很多,需要时间。 SqlCommand的默认超时时间为15秒。

如果你尝试做什么

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

这会禁用连接超时,并允许更多时间执行填充。尝试调整它(时间以毫秒为单位),如果出现问题,不要让它无限期地运行。

如果没有,请尝试使用

设置Web服务的操作时间
Webservice.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(10);

将其设置为更长的时间跨度,因为默认的wcf时间跨度是1:00,这就是发生在你身上的事情。