Click here to Skip to main content
13,298,965 members (37,621 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 26 Jun 2009

WCF Large File Download Upload Service

, 26 Jun 2009
Rate this:
Please Sign up or sign in to vote.
A WCF document mangement service for downloading and uploading large files, hosted on a Windows Service.


Any LOB application requires document attachment facility; the user will demand to attach documents with records (i.e., scanned copies of sales invoices, purchase invoices etc.). So, it is required to provide a common facility to upload and download documents. Here I am providing an implementation of a TCP binding WCF service which is hosted in Windows Services. This service can be invoked by any WPF / WinForms based client application.

I will divide the solution in four parts:

  1. Interface Creation
  2. Service Creation
  3. Hosting Service (Windows Services)
  4. Consuming (Client)

Interface Creation

This is just a simple interface with two methods; first, it is required to reference and import the "System.ServiceModel" assembly. Here, I am creating a different project for the interface as we can use the same DLL in the service class as well as the client.

public interface IDocumentLibraryService
    void UploadDocument(string fPath, byte[] data);
    byte[] DownloadDocument(string fPath);

Service Creation

As we are creating a WCF service, it is required to add a reference to the interface "IDocumentLibraryService". So, after adding the reference, I have inherited the DocumentLibraryService class from that interface and implemented both the methods.

Upload Document

The code to process the incoming stream is rather simple; storing the uploaded files to a single folder on the disk and overwriting any existing files.

Download Document

Open the file, convert its contents into a byte array, and return it to the calling process /client.

public class DocumentLibraryService : IDocumentLibraryService.IDocumentLibraryService 
   public void UploadDocument(string fPath, byte[] data)
      string filePath = fPath;
      FileStream fs = new FileStream(filePath, FileMode.Create, 
      fs.Write(data, 0, data.Length);
   public byte[] DownloadDocument(string fPath)
      string filePath = fPath;
      // read the file and return the byte[
      using (FileStream fs = new FileStream(filePath, FileMode.Open, 
                                 FileAccess.Read, FileShare.Read))
          byte[] buffer = new byte[fs.Length];
          fs.Read(buffer, 0, (int)fs.Length);
          return buffer;

Hosting Service

Create a Windows Service project (optionally, you can also host your service in a normal WinForms or WPF application), add the above created service class, and open the channel in the OnStart method of the Windows Service. Close the channel in the OnStop method. Here, I set the number of binding properties before opening the channel to make sure it works in the case of large files too. Timeout property settings are required because if the client takes longer to upload a file it does not come up with a timeout exception.

To be honest, I have not played with these settings very much yet, so you will probably want to tweak these to your own situations. After setting up all the properties, I add the endpoint to the service. I have selected the port 8000; in your case, you can select any.

Finally, the server is waiting for any client to create the channel at port no. 8000.

Note: If you have installed any firewall, anti virus, Windows firewall etc., then please make the exception entry into it for your selected port. Also, the created Windows Service must have the security rights to access the specified folder or location on the disk.

To know more about how to create a Windows Service:

protected override void OnStart(string[] args)
    // TODO: Add code here to start your service.

void StartWCFService()
        NetTcpBinding ntcp = new NetTcpBinding();
        ntcp.MaxBufferPoolSize = 2147483647;
        ntcp.MaxReceivedMessageSize = 2147483647;
        ntcp.MaxBufferSize = 2147483647;
        ntcp.ReaderQuotas.MaxStringContentLength = 2147483647;
        ntcp.ReaderQuotas.MaxDepth = 2147483647;
        ntcp.ReaderQuotas.MaxBytesPerRead = 2147483647;
        ntcp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
        ntcp.ReaderQuotas.MaxArrayLength = 2147483647;
        ntcp.SendTimeout = new TimeSpan(1, 10, 0);
        ntcp.ReceiveTimeout = new TimeSpan(1, 10, 0);
        svh = new ServiceHost(typeof(DocumentLibraryService));
           svh.Description.Behaviors[0]).MaxItemsInObjectGraph = 2147483647;
    catch (Exception e)

Consuming (Client)

Here, firstly, I have to add a reference to our interface DLL. After that, I have created a channel between the client and the service after setting up all the properties for the binding.

ChannelFactory<IDocumentLibraryService.IDocumentLibraryService> scf;
scf = new ChannelFactory<IDocumentLibraryService.IDocumentLibraryService>(ntcp, 
IDocumentLibraryService.IDocumentLibraryService client;
client = scf.CreateChannel();
  • Download: After calling the downloaddocument function with the required parameters, it is required to save the downloaded document to a user defined location.
  • Upload: Here, first, we will ask the user for the file path to upload a document, and after converting that document into a byte array, we will pass it to the uploaddocument function.


As you can see, it is very easy to create a document download / upload WCF service which can be used across multiple applications. I have not created the UI for the client, i.e., the one for asking the user for the file name to upload or download etc..

Please provide your valuable suggestions / criticisms / feedback.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader Phoenix Tech Labs
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
leiyangge19-Aug-14 5:29
memberleiyangge19-Aug-14 5:29 
GeneralMy vote of 5 Pin
v green15-Oct-12 7:02
memberv green15-Oct-12 7:02 
QuestionQuestion Pin
varun surana18-Oct-11 2:26
membervarun surana18-Oct-11 2:26 
GeneralMy vote of 2 Pin
.Shoaib9-Aug-11 4:55
member.Shoaib9-Aug-11 4:55 
GeneralMy vote of 1 Pin
rageit13-Jun-11 11:24
memberrageit13-Jun-11 11:24 
GeneralMy vote of 1 Pin
vrsanaidu30-Jun-10 0:43
membervrsanaidu30-Jun-10 0:43 
GeneralMy vote of 1 Pin
Member 317768417-Jan-10 14:58
memberMember 317768417-Jan-10 14:58 
GeneralMy vote of 1 Pin
JO Hyeong-Ryeol8-Dec-09 22:29
memberJO Hyeong-Ryeol8-Dec-09 22:29 
GeneralMy vote of 1 Pin
NightDorian6-Aug-09 5:16
memberNightDorian6-Aug-09 5:16 
GeneralLarge video files upload (file size over 10 gb) Pin
Sharmistha Sen16-Jul-09 4:22
memberSharmistha Sen16-Jul-09 4:22 
GeneralMy vote of 1 Pin
Anderson Imes7-Jul-09 6:26
memberAnderson Imes7-Jul-09 6:26 
GeneralWCF Streaming Pin
Anderson Imes7-Jul-09 6:25
memberAnderson Imes7-Jul-09 6:25 
GeneralMy vote of 1 Pin
JaredFox30-Jun-09 11:52
memberJaredFox30-Jun-09 11:52 
GeneralMy vote of 2 Pin
WillemToerien29-Jun-09 3:54
memberWillemToerien29-Jun-09 3:54 
GeneralNot a good idea Pin
WillemToerien29-Jun-09 3:53
memberWillemToerien29-Jun-09 3:53 
GeneralMy vote of 2 Pin
rcardare27-Jun-09 3:44
memberrcardare27-Jun-09 3:44 
GeneralMy vote of 1 Pin
_FleX26-Jun-09 5:01
member_FleX26-Jun-09 5:01 
GeneralRe: My vote of 1 Pin
HarishBhattbhatt26-Jun-09 5:16
memberHarishBhattbhatt26-Jun-09 5:16 
GeneralRe: My vote of 1 Pin
dchrno26-Jun-09 5:52
memberdchrno26-Jun-09 5:52 
GeneralRe: My vote of 1 Pin
HarishBhattbhatt26-Jun-09 22:35
memberHarishBhattbhatt26-Jun-09 22:35 
GeneralRe: My vote of 1 Pin
Daniel Cazzulino [XML MVP]21-Feb-10 11:26
memberDaniel Cazzulino [XML MVP]21-Feb-10 11:26 
GeneralRe: My vote of 1 Pin
Druuler26-Jun-09 8:45
memberDruuler26-Jun-09 8:45 
GeneralRe: My vote of 1 Pin
Guillaume Leparmentier26-Jun-09 20:44
memberGuillaume Leparmentier26-Jun-09 20:44 
Generalquestion Pin
Savara26-Jun-09 4:18
memberSavara26-Jun-09 4:18 
GeneralRe: question Pin
HarishBhattbhatt26-Jun-09 22:42
memberHarishBhattbhatt26-Jun-09 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 26 Jun 2009
Article Copyright 2009 by HarishBhattbhatt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid