我在项目中使用了WCF、LINQ等技术,具体脚本如下:首先是定义服务Service方法:
        public DataTable GetAllDeptInfo()
        {
            using (GxkcERPContext gxkcERPContext = new GxkcERPContext())
            {
                //IEnumerable<basDepartment> dept = from d in gxkcERPContext.basDepartments
                //                                  select d;
                var dept = from d in gxkcERPContext.basDepartments
                                                  join d1 in gxkcERPContext.basDepartments on d.FParentID equals d1.DepartmentID into deptTable
                                                  from d1 in deptTable.DefaultIfEmpty()
                                                  select new
                                                  {
                                                      d.DepartmentID,
                                                      单位编码 = d.DepartmentCode,
                                                      单位名称 = d.DepartmentName,
                                                      全称 = d.DepartmentFullName,
                                                      d.FParentID,
                                                      所属单位 = d1.DepartmentName,
                                                      d.FCancel
                    
                                                  };                return dept.ToADOTable(rec => new object[] { dept });
            }        }
然后是服务接口方法:
        //取全部单位记录:
        [OperationContract]
        //basDepartment[] GetAllDeptInfo();
        DataTable GetAllDeptInfo();
最后是客户端的执行方法:
        private void loadallinfo()
        {
            using (BasicServiceClient basDepartment = new BasicServiceClient())
            {                
                List<basDepartment> listdept = new List<basDepartment>();                //basDepartment[] dept = basDepartment.GetAllDeptInfo();//这种类型正常                DataTable dt = basDepartment.GetAllDeptInfo();
                
                //listdept.AddRange(dept);//用这两句正错
         //dgv_dept.DataSource = listdept;                  dgv_dept.DataSource = dt;                             
            }
        }
当执行客户端时出现以下的错误提示:“An error occurred while receiving the HTTP response to http://localhost:8080/GxkcERP.BasicService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.”
这里提示是通讯协议不对,我把HTTP换成TC/IP也不对。
注意:在定义服务方法时我把返回方法设计成DataTable类型就不行,如果采用数组类型就没问题,如下面的脚本就正常,(basDepartment是一个Table)。
        public basDepartment[] GetAllDeptInfo()
        {
            using(GxkcERPContext gxkcERPContext = new GxkcERPContext())
            {
            basDepartment[] dept = (from d in gxkcERPContext.basDepartments
                                        select d).ToArray();                return dept;
            }        }
但如果用basDepartment[]定义最前面的服务方法时系统会报错,因为方法输出的结构与basDepartment的结构不一样了。请老大帮忙提供一个解决办法,小的不胜感激!

解决方案 »

  1.   

    看一下日志或调试一下,看是否有更详细的异常信息在SOA里,实体是推荐的用法,问一下自己,1.为什么要返回DataTable?
    2.要返回DataTable的话,为什么不返回强类型的DataTable?
    3.试过返回DataSet么?
      

  2.   

    又发现一个用了Linq还抱着DataTable不放的
      

  3.   

    老大您好,顺便再问一个问题:(C#)怎样通过LINQ进行Image类型的字段操作,如保存到数据库、从数据库取出并显示?
      

  4.   

    3楼的兄弟,如果只用LINQ,不用DataTale或DataSet又怎样解决上面的问题,我用 IEnumerable<>或数组是不行的,请赋教!
      

  5.   

    我碰到类似的问题,但返回dataset就没问题,返回datatable就不行,不知道是什么原因
      

  6.   

    datatable只支持二进制序列化
    你可能是xml序列化吧