WCF 傳輸大資料的問題 轉

來源:互聯網
上載者:User

使用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 就不要試了。。。記憶體太小了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.