WCF 传输大数据的问题 转 武胜

使用WCF的默认DataContractSerializer手动去序列化成byte[],然后接收后再手动去反序列化,能解决这个问题。也就是说单纯的byte[]能过去,直接将下面代码中的list以List<May>返回去就是出现LZ遇到的问题。

也就是说序列化与反序列化这一大块数据都没问题。主要问题还是出现在WCF组装消息上了。
设置一下 ReaderQuotas 这个属性,这是设置消息复杂性的。
感觉这种症状很像被DOS干掉的感觉,于是想到ReaderQuotas。

下面是我尝试的例子。

C# code
publicbyte[] GetMays() { DataContractSerializer DCZ =new DataContractSerializer(typeof(List<May>)); List<May> list =new List<May>(); for (int i =0; i <30000; i++) { May tmp =new May { Name = DateTime.Now.ToString("yyyy-MM-dd") }; list.Add(tmp); } using (MemoryStream fs =new MemoryStream()) { DCZ.WriteObject(fs, list); return fs.ToArray(); } }
-------------------
用你这个方法搞定。客户端还要设置下
  netTcpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
  netTcpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
  netTcpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;


//-------------------------------
  System.Diagnostics.Stopwatch myWatch = new System.Diagnostics.Stopwatch();
  myWatch.Start();
  // TaxiInfo[] taxiInfos = PositionService.GetAllTaxiInfos();
  byte[] sds = PositionService.GetMays();
  myWatch.Stop();
  Console.WriteLine("耗时:" + myWatch.ElapsedMilliseconds + "ms");

  MemoryStream memory = new MemoryStream(sds);
  XmlDictionaryReader reader =
  XmlDictionaryReader.CreateTextReader(memory, new XmlDictionaryReaderQuotas());
  DataContractSerializer ser = new DataContractSerializer(typeof(List<TaxiInfo>));
  // Deserialize the data and read it from the instance.
  List<TaxiInfo> deserializedPerson =
  (List<TaxiInfo>)ser.ReadObject(reader, true);
  reader.Close();
  // Console.WriteLine(deserializedPerson);

这样就没问题了。3Q
----------------------
怀疑还是别的问题。。。我测试10000都没有问题呀。

WcfLibrary:
1. 契约:
 
C# code
[DataContract] publicclass TaxiInfo { [DataMember] publicstring PhoneNumber { get; set; } [DataMember] publicstring Others { get; set; } } [ServiceContract] publicinterface IService1 { [OperationContract] List<TaxiInfo> GetAllTaxiInfos(); }

2. Service
C# code
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)] publicclass Service1 : IService1 { privatestatic List<TaxiInfo> _taxis =new List<TaxiInfo>(); static Service1() { foreach (var i in Enumerable.Range(1, 10000)) { var taxi =new TaxiInfo { PhoneNumber = i.ToString().PadLeft(12, '0') }; _taxis.Add(taxi); } } public List<TaxiInfo> GetAllTaxiInfos() { return _taxis; } }


3. Winform Host
 
C# code
publicpartialclass Form1 : Form { public Form1() { InitializeComponent(); } private ServiceHost _host; privatevoid button1_Click(object sender, EventArgs e) { NetTcpBinding netTcpBinding =new NetTcpBinding(SecurityMode.None, true) { MaxBufferPoolSize =2147483647,//2g MaxBufferSize =2147483647, MaxReceivedMessageSize =2147483647, SendTimeout =new TimeSpan(0, 0, 30), ReceiveTimeout =new TimeSpan(20, 0, 10), ReliableSession = { Enabled =true, InactivityTimeout =new TimeSpan(20, 0, 10) } }; try { _host =new ServiceHost(typeof(WcfServiceLibrary1.Service1)); ServiceThrottlingBehavior throttlingBehavior = _host.Description.Behaviors.Find<ServiceThrottlingBehavior>(); if (throttlingBehavior ==null) { throttlingBehavior =new ServiceThrottlingBehavior { MaxConcurrentCalls =3000, MaxConcurrentSessions =3000 }; _host.Description.Behaviors.Add(throttlingBehavior); } else { throttlingBehavior.MaxConcurrentCalls =3000; throttlingBehavior.MaxConcurrentSessions =3000; } //_host.Description.Endpointsstring strAddress ="net.tcp://localhost:20000/PositionServices"; _host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), netTcpBinding, strAddress); _host.Open(); label1.Text ="Service is opened..."; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }


3. WcfClient
 
C# code
class Program { privatestatic WcfServiceLibrary1.IService1 PositionService =null; staticvoid Main(string[] args) { try { InitConnect(); var sw =new Stopwatch(); sw.Start(); var allTaxis = PositionService.GetAllTaxiInfos(); Console.WriteLine(allTaxis.Count); sw.Stop(); Console.WriteLine("GetAllTaxiInfos Elapsed:{0}ms", sw.ElapsedMilliseconds); Console.Read(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } staticvoid InitConnect() { NetTcpBinding netbinding =new NetTcpBinding(SecurityMode.None, true) { MaxBufferPoolSize =2147483647, MaxBufferSize =2147483647, MaxReceivedMessageSize =2147483647, SendTimeout =new TimeSpan(0, 0, 30), ReceiveTimeout =new TimeSpan(20, 0, 0), ReliableSession = { Enabled =true, InactivityTimeout =new TimeSpan(20, 0, 10) }, }; PositionService = ChannelFactory<WcfServiceLibrary1.IService1>.CreateChannel (netbinding, new EndpointAddress("net.tcp://localhost:20000/PositionServices")); } }


输出:
10000
GetAllTaxiInfos Elapsed:2183ms

-------------------------
服务端配置一下就可以。确实如有网友说的,每次数据不要太大,建议如果数据大的话用异步分割成小块去处理,当然如果是结合数据库的话,分页最好是在服务端做。
序列化肯定很耗内存的.
 
---------
改到 100000 也没错

100000
GetAllTaxiInfos Elapsed:1160ms


没问题啊,你出什么错误?

OutOfMemoryException 就不要试了。。。内存太小了。
原文地址:https://www.cnblogs.com/zeroone/p/2447929.html