I have written WCF service methods that return large string data as below:
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();
}
User class:
[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; }
}
When I try to run the service it throws the following exception:
strData.ToString()
'strData开发者_开发技巧.ToString()' threw an exception of type 'System.OutOfMemoryException string {System.OutOfMemoryException}
And when I add service reference and show data to a grid view it also shows error.
What is the right approach to return very big string from WCF service?
The main problem with trying to grab so much data at once is the massive overhead of desearializing all that data. I would break up the data into more manageable chunks on the server side, and make repeated requests to the service for the next chunk, until there are no chunks left.
I also don't know why you are doing the serialization yourself. I would simply create a data contract to hold your data, and let WCF handle the serialization / deserializeation automatically :)
EDIT:
[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;
}
精彩评论