hit tracker

WCF

Home » WCF

Transfer a Large Data from Client to WCF and Vice Versa in .Net C#

in C-Sharp, Programming by Balamanigandan B Comments are off

C# is one of the most popular languages today and there are many reasons for this. It has many qualities that make it suitable for development for users at most all levels. Let’s discuss how efficiently transfer a large data from Client to WCF and Vice Versa Using .Net C#.

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, we can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.

Plan your Data usage between WCF and Client before you start your project. Then use the appropriate MessageEncoding and transferMode in your WCF web.config

Let us see the default web.config in WCF

and the default App.config in a Client Application

The default MessageEncoding is “normal text XML” and the transferMode is “Buffered” – Buffered transfers hold the entire message in a memory buffer until the transfer is complete.

In the above configuration the MessageEncoding and transferMode is not defined, but the system by default taken it as

The above configuration does not transfer the file with the size  ≈>18 MB from and to the WCF. During the analysis the maxReceivedMessageSize=”2147483647″ all are seems good. But the default MessageEncoding is “normal text XML” and the transferMode is “Buffered” – Buffered transfers hold the entire message in a memory buffer until the transfer is complete. So, it does not support for the Large Bytes in terms of memory and transport. The Communication may get terminate depends upon the Network Traffic or any other circumstances. Due to this reason the application may get crashed.

So, we need to change the MessageEncoding to “MTOM” (Message Transmission Optimization Mechanism). (MTOM) is an efficient technology for transmitting binary data in Windows Communication Foundation (WCF) messages. The MTOM encoder attempts to strike a balance between efficiency and interoperability. The MTOM encoding transmits most XML in textual form, but optimizes large blocks of binary data by transmitting them as-is, without conversion to text.

Additionally we have to change the transferMode to “StreamedRequest” or “Streamed”. StreamedRequest gives better proformance then Streamed. The request message is streamed and the response message is buffered in StreamedRequest.

Let us consider the modified version of the web.config, it will support the large data transfer. Binding information should be inside the system.ServiceModel of Web.config in WCF Service

and the EndPoint should be

readerQuotas :

It is WCF level setting, especially SOAP message and not about overall length of request. It puts various restrictions on the size of SOAP messages which can be processed by WCF endpoint. Those settings basically needed to prevent various kinds of a denial-of-service (DoS) attack against the service using xml messages prepared in a special way.

But we need some more permission in web server level, so we have to tune setting in web server level, they are

requestLimits  :

It is a web server level setting. When request comes with ContentLength, i.e., it purely speaks about the URL length. It rejects the request with 404 error (Page not available) only if exceeds the limit which is specified in the property maxAllowedContentLength, it will not even get into WCF pipeline. So, this configuration setting is not related to WCF at all. Note that it limits the overall length of request; whatever is inside the request does not matter.

The Completed Version of web.config in a WCF should be

and the Completed Version of App.config in a Client Application should be

Here the endpoint address is refereeing the hosted address of a WCF, via this address the client application consumes the service from the WCF.

Conclusion: This article targets at understanding how efficiently we can transfer large amount of data from a Client application to WCF and vice versa. This article is intended for the beginner/intermediate level. I hope, this post will solve all your basic functionality implementation of WCF Configuration. Think Big… Start Small… Do Fast…