【问题标题】:Why is my WCF Service timing out when I try to fill my datalist?当我尝试填写数据列表时,为什么我的 WCF 服务会超时?
【发布时间】:2016-10-19 20:25:37
【问题描述】:

我有一个连接到 wcf 服务的 MVC 应用程序,我正在尝试制作一个包含依赖于其他任务的任务的待办事项列表应用程序,到目前为止我没有运气,但我已经构建了一个存储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();
            }
        }

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

提前感谢:)

【问题讨论】:

  • 你独立测试过存储过程吗?它运行吗?同时发布您的 WCF 接口和合同。

标签: c# asp.net wcf datatable


【解决方案1】:

可能返回的数据很多,需要时间。 SqlCommand 有 15 秒的默认超时时间。

如果你尝试做什么

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

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

如果没有,尝试设置webservice的操作超时

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

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

【讨论】:

  • sqlcommand 显然不包含 ConnectionTimeout 的定义
  • 可能是因为我要返回一个数据表吗?
  • @ReeceCottam 我不知道。到底是什么?你解决了吗?
  • @ReeceCottam 更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 2013-07-29
  • 1970-01-01
  • 2012-03-25
  • 2011-03-10
相关资源
最近更新 更多