How to transfer a large amount of data using WCF?

We are currently trying to move large amounts of data to a Silverlight 3 client using WCF using PollingDuplex. I read about MultiplerMessagesPerPoll in Silverlight 4, and it seems to be a little faster. Are there any examples for me to reference (using MultipleMessagesPerPoll)? Or maybe good links to using Net.TCP? Maybe I should take a completely different approach? Any ideas or suggestions would be greatly appreciated.

Thanks!

+4
source share
3 answers

Stream serialization of response blocks works well:

The WCF binding configuration will look like this:

<binding name="myCustomBinding"> <binaryMessageEncoding /> <httpTransport transferMode="StreamedResponse" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" /> </binding> 

Your service method will look something like this:

 [OperationContract] public Stream GetDataStream(string objectId) { Stream stream = new MemoryStream(); MyObject obj = Manager.GetObject(objectId); DataContractSerializer serilizer = new DataContractSerializer(typeof(MyObject)); serilizer.WriteObject(stream, obj); stream.Position = 0; return stream; } 

And your completed client-side method will do the following:

 static void client_GetDataStreamCompleted(object sender, GetDataStreamCompletedEventArgs e) { if (e.Error == null) { DataContractSerializer serializer = new DataContractSerializer(typeof(MyObject)); MyObject obj = serializer.ReadObject(new MemoryStream(e.Result)) as MyObject; } } 
+2
source

I implemented the proposed solution above. After implementation, I found this link:

http://msdn.microsoft.com/en-us/library/ms752244.aspx

Then I implemented binary notation as below.

Service Method:

 [OperationContract] public Stream GetAllLocationsDataStream(string customerId) { Stream stream = new MemoryStream(); try { Customer customer = ServiceEquipmentManager.GetCustomerAllLocations(customerId); DataContractSerializer serializer = new DataContractSerializer(typeof(Customer)); XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream); serializer.WriteObject(binaryDictionaryWriter, customer); binaryDictionaryWriter.Flush(); } catch (Exception ex) { string timestamp; ExceptionHelper.HandleExceptionWrapper(ex, "Log Only", out timestamp); } stream.Position = 0; return stream; } 

Completed event on the client side:

 XmlDictionaryReader binaryDictionaryReader = XmlDictionaryReader.CreateBinaryReader(new MemoryStream(e.Argument as byte[]), XmlDictionaryReaderQuotas.Max); Customer customer = serializer.ReadObject(binaryDictionaryReader) as Customer; 

I checked the difference of my object, as shown in the link above, my results are shown below:


Text = 68,866,216 bytes


Binary = 49,207,475 bytes (28.5% less text)

+1
source

I base this on your answer and its emphasis on the amount of data transferred. If you save data blocks transferred under 4 GB, you can use the GZipStream class in the System.IO.Compression namespace. In my experience using it in plain text, it reduced the data stream to about 17-20% of its original size.

0
source

Source: https://habr.com/ru/post/1308661/


All Articles