我编写了返回大字符串数据的WCF服务方法,如下所示:
string IEmpService.GetEmployeeInfo()
{
StringBuilder strData = new StringBuilder();
//create list of user class
List<Users> lstUsers = new List<Users>();
// populate 10000000 data from DB table at a time
_ods = populateEmpData();// data store to dataset
// convert dataset to list
lstUsers = (from dr in _ods.Tables[0].AsEnumerable()
select new Users
{
UsersID = dr.Field<int>("UsersID"),
UserName = dr.Field<string>("UserName"),
Password = dr.Field<string>("Password")
}).ToList();
// do Serialize of above list and store it to a string builder(this create huge amount of string dada)
strData.Append(Utility.DeSerilization.comonMapper.SerializeList<List<Users>>(lstUsers));
return strData.ToString();
}
用户类:
[Serializable]
[XmlRootAttribute(ElementName = "Users", IsNullable = false)]
public class Users
{
[XmlElement("UsersID")]
public int UsersID { get; set; }
[XmlElement("UserName")]
public string UserName { get; set; }
[XmlElement("Password")]
public string Password { get; set; }
}
当我尝试运行该服务时,它会引发以下异常:
strData.ToString()
'strData.ToString()'引发了类型异常 'System.OutOfMemoryException string {System.OutOfMemoryException}
当我向网格视图添加服务引用和显示数据时,它也会显示错误。
从WCF服务返回非常大的字符串的正确方法是什么?
答案 0 :(得分:0)
尝试一次抓取这么多数据的主要问题是将所有数据去重新分类的大量开销。我会在服务器端将数据拆分为更易于管理的块,并为下一个块重复请求服务,直到没有剩余块为止。
我也不知道你为什么要自己进行序列化。我只需创建一个data contract来保存您的数据,让WCF自动处理序列化/反序列化:)
编辑:
[DataContract]
public class Users
{
[DataMember]
public int UsersID { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string Password { get; set; }
}
public List<Users> IEmpService.GetEmployeeInfo()
{
//create list of user class
List<Users> lstUsers = new List<Users>();
// populate 10000000 data from DB table at a time
_ods = populateEmpData();// data store to dataset
// convert dataset to list
lstUsers = (from dr in _ods.Tables[0].AsEnumerable()
select new Users
{
UsersID = dr.Field<int>("UsersID"),
UserName = dr.Field<string>("UserName"),
Password = dr.Field<string>("Password")
}).ToList();
return lstUsers;
}