简体   繁体   English

从 asp.net 中的 wcf 服务返回大字符串数据

[英]return large string data from wcf service in asp.net

I have written WCF service methods that return large string data as below:我编写了返回大字符串数据的 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(); 
}

User class:用户 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()
'strData.ToString()' threw an exception of type 'System.OutOfMemoryException string {System.OutOfMemoryException} “strData.ToString()”引发了“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?从 WCF 服务返回非常大的字符串的正确方法是什么?

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:)我只需创建一个数据合约来保存您的数据,然后让 WCF 自动处理序列化/反序列化:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM