WCF 传输大数据的问题 转

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

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

下面是我尝试的例子。

C# code
public byte [] 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] public class TaxiInfo { [DataMember] public string PhoneNumber { get ; set ; } [DataMember] public string Others { get ; set ; } } [ServiceContract] public interface IService1 { [OperationContract] List < TaxiInfo > GetAllTaxiInfos(); }

2. Service
C# code
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] public class Service1 : IService1 { private static 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
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private ServiceHost _host; private void 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.Endpoints string 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 { private static WcfServiceLibrary1.IService1 PositionService = null ; static void 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); } } static void 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/archive/2012/04/15/2447929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值