Click here to Skip to main content
15,301,414 members
Articles / Programming Languages / Visual Basic
Posted 6 Sep 2007


244 bookmarked

Progress Indication while Uploading/Downloading Files using WCF

Rate me:
Please Sign up or sign in to vote.
4.92/5 (66 votes)
4 Feb 2009CPOL5 min read
This article examines the implementation of upload and download functionality with progress indication (progress bar feature) using Windows Communication Foundation.
Screenshot - WCF_FileTransfer_Progress.png


This article examines the implementation of upload and download functionality with progress indication (progress bar feature) using the Windows Communication Foundation. For this sample, you will need Visual Studio 2008.

The sample code consists of three projects bundled in a solution. A brief description of these projects follows. The attached sample code is available in C# and VB.NET (conversion to VB.NET was made by Lee Galloway of Project Time and Cost).


This is the main server project.

The Server Contract

The File Server project includes FileTransferServiceContract.cs file, which contains the IFileTransferService interface. This interface describes the operations provided by our server. No actual work is done in this code file except in describing the operations provided. If you've worked with service-oriented applications before, you'll know that this job is important enough to spare a separate file for. Here are the two operations of our file transfer service:

  • DownloadFile Server Method

    Accepts a DownloadRequest instance that contains the name of the file to be downloaded by the client. It returns a RemoteFileInfo instance, defined in the same code file. RemoteFileInfo contains the name of the file to be downloaded, the file stream and the length of the file in bytes. This instance of the RemoteFileInfo class will be used by the client to download the file. You notice that filename and length are marked with the MessageHeader attribute in the RemoteFileInfo class. This is because when a message contract contains a stream, this can be the only body member of the contract.

  • UploadFile Server Method

    Accepts an instance of the RemoteFileInfo message contract. This is the same as used in DownloadFile, only in this case the length property is not required.


public interface IFileTransferService
    void UploadFile(RemoteFileInfo request);

    RemoteFileInfo DownloadFile(DownloadRequest request);

public class RemoteFileInfo
    [MessageHeader(MustUnderstand = true)]
    public string FileName;

    [MessageHeader(MustUnderstand = true)]
    public long Length;

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream FileByteStream;


<ServiceContract()> _
<servicecontract() />Public Interface IFileTransferService

    <OperationContract()> _
    Sub UploadFile(ByVal request As RemoteFileInfo)

    <OperationContract()> _
    Function DownloadFile(ByVal request As DownloadRequest) As RemoteFileInfo

End Interface

<MessageContract()> _
Public Class RemoteFileInfo
    Implements IDisposable

    <OperationContract()> _
    Public FileName As String

    <OperationContract()> _
    Public Length As Long

    <MessageBodyMember(Order:=1)> _
    Public FileByteStream As System.IO.Stream

End Class

The Server Implementation

File Server also includes the FileTransferService.cs code file which contains the implementation of the contract, i.e. the actual code that does all the work. Apparently the included class implements the IFileTransferService class, which constitutes the service contract. If you have worked with streams before in .NET, you will find out that the code that handles the stream and related information for upload or download is pretty straightforward. If you are new to .NET streams, please use Google for a quick introduction.

Note here that since actual downloading of the file starts after the execution of the DownloadFile method is completed (i.e. after the client gets the RemoteFileInfo instance returned by this method), the server must close the opened stream later, after the client completes the process. An elegant approach was suggested by Buddhike. To do this, the IDisposable interface is implemented by the RemoteFileInfo contract and the stream is disposed on the corresponding Dispose method. If this is not done, the stream will remain locked and the corresponding file will be locked for writing.


FileService is a class library and hence it cannot start as a window process. Therefore it needs another executable file-process that will host it. Several types of processes can host a WCF service, such as .NET executables, IIS processes, Windows Activation Services (new feature of Vista) and many more. Our example uses a .NET executable to host our service. So, ConsoleHost is a console application that does exactly this. It has a reference to the FileService project. However, it is not related in any way with the business our service is doing, i.e. transferring files. Actually, the code you will find in Program.cs would be the same even if our service was designed to host an online grocery. Take a quick look at this code file to understand how our service is started and closed.


static void Main(string[] args)
    ServiceHost myServiceHost = new ServiceHost

    Console.WriteLine("This is the SERVER console");
    Console.WriteLine("Service Started!");
    foreach (Uri address in myServiceHost.BaseAddresses)
        Console.WriteLine("Listening on " + address.ToString());
    Console.WriteLine("Click any key to close...");



Public Shared Sub Main()

    Dim myServiceHost As New ServiceHost(_

    Console.WriteLine("This is the SERVER console")
    Console.WriteLine("Service Started!")
    For Each address As Uri In myServiceHost.BaseAddresses
        Console.WriteLine("Listening on " + address.ToString())
    Console.WriteLine("Click any key to close...")


End Sub

The configuration of ConsoleHost is what matters the most! It is divided into three sections, configuring the way our service will behave and how it will be exposed to the rest of the world. It is not the goal of this article to describe in detail how a WCF service is configured, so please refer to the WCF reference on MSDN for more information. Something noticeable in the configuration of our service is that it uses MTOM as message encoding and stream as transfer mode. See also the maxReceivedMessageSize property. This defines the maximum size of messages transferred by our service. Since we are transferring files, we want this property to have a large value.


<binding name ="FileTransferServicesBinding"
    maxReceivedMessageSize="10067108864" >


The Client project is a sample consumer of our service. You will notice that the Client project includes a folder called Service References. This folder contains a bunch of files created automatically by Visual Studio by right clicking on the Client project root and selecting "Add Service Reference." The files in this folder are the proxy of our file transfer service on the client side. Client is using these files to send requests to the server, hiding in this way the complexity of Web Service and SOAP protocols.

Again, if you have worked with streams before, you will notice that things are pretty simple in the TestForm file except for one small part, which is also the difference in implementing the progress indication when uploading rather than when downloading. When downloading, the client has control of the procedure. You can see in TestForm.cs that downloading is implemented using a loop that reads the server stream piece-by-piece. So, the client knows what part of the server stream is read and how many more remain. When uploading, that loop resides on the server. In order for the client to know how many bytes the server read, it uses the StreamWithProgress class, which inherits System.IO.Stream. An instance of this class is passed to the server, instead of the original file stream. Since this class overrides the default Read method of the stream (see code below), it can report the progress of the uploading process to the client!


public override
    int Read(byte[] buffer, int offset,
    int count)
    int result = file.Read(buffer, offset, count);
    bytesRead += result;
    if (ProgressChanged != null) 
    ProgressChanged(this, new ProgressChangedEventArgs(bytesRead, length));
    return result;


Public Overloads Overrides Function Read(ByVal buffer As Byte(), _
    ByVal offset As Integer, ByVal count As Integer) As Integer
    Dim result As Integer = file.Read(buffer, offset, count)
    bytesRead += result
    RaiseEvent ProgressChanged(Me, New ProgressChangedEventArgs(_
        bytesRead, m_length))
    Return result
End Function


  • Updated on 2007/09/09: A more elegant approach for the server implementation was suggested by Buddhike.
  • Updated on 2007/10/24: Code now also provided in VB.NET. Conversion made by Lee Galloway of Project Time and Cost.
  • Updated on 2009/02/03:
    • Upgraded projects to Visual Studio 2008 (.NET Framework 2.0 is still the target framework).
    • Properly closing read stream on client when download is done. Failure to do this was causing the client to throw timeout exceptions after 2 or 3 downloads.


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


About the Author

Dimitris Papadimitriou
Software Developer (Senior)
Luxembourg Luxembourg
Dimitris Papadimitriou is a Software Development Professional specialized in Microsoft technologies. He has been awarded with the Microsoft MVP award (Connected System Developer). He lives in Luxembourg. Read more in his personal web page.

Comments and Discussions

QuestionMTOM , streaming ? Pin
kiquenet.com19-Mar-19 7:03
professionalkiquenet.com19-Mar-19 7:03 
Questionhave a proble when try open this project Pin
EdwinJose11-Jul-16 12:43
MemberEdwinJose11-Jul-16 12:43 
QuestionHow do I use it for other objects ?! Pin
vpantaleo30-Jun-16 5:48
Membervpantaleo30-Jun-16 5:48 
QuestionIs it possible to improve this solution to support uploading and downloading with multiple files in one time? Pin
the__fairy23-Nov-14 5:25
Memberthe__fairy23-Nov-14 5:25 
AnswerRe: Is it possible to improve this solution to support uploading and downloading with multiple files in one time? Pin
the__fairy24-Nov-14 21:08
Memberthe__fairy24-Nov-14 21:08 
QuestionThe stream is not passing to WCF Pin
Just Kiran22-Nov-14 0:49
MemberJust Kiran22-Nov-14 0:49 
QuestionCode Pin
Roberto Pasquali8-Aug-14 0:43
MemberRoberto Pasquali8-Aug-14 0:43 
QuestionException error when uploading the file from network Pin
saugat.kc14-Jul-14 22:53
Membersaugat.kc14-Jul-14 22:53 
QuestionTrue Progress shown? Pin
Member 1037933818-Feb-14 9:55
MemberMember 1037933818-Feb-14 9:55 
AnswerRe: True Progress shown? Pin
Member 1037933818-Feb-14 10:32
MemberMember 1037933818-Feb-14 10:32 
GeneralRe: True Progress shown? Pin
racing576-Jan-15 23:06
Memberracing576-Jan-15 23:06 
BugThis Upload Method will need more than the actual uploading time Pin
Murhaf Suz12-Nov-13 4:07
MemberMurhaf Suz12-Nov-13 4:07 
GeneralA big thanks Pin
Thales Sarczuk4-Oct-13 10:48
MemberThales Sarczuk4-Oct-13 10:48 
GeneralMy vote of 5 Pin
vadim.xn12-Sep-13 1:22
Membervadim.xn12-Sep-13 1:22 
QuestionStream Read always return 0 Pin
devser13-Aug-13 21:30
Memberdevser13-Aug-13 21:30 
QuestionHow use svcutil.exe file? Pin
markmao_love16-Oct-12 16:55
Membermarkmao_love16-Oct-12 16:55 
GeneralMy vote of 5 Pin
Member 372567114-Oct-12 20:43
MemberMember 372567114-Oct-12 20:43 
GeneralMy vote of 5 Pin
terrykaoru3-Jul-12 22:53
Memberterrykaoru3-Jul-12 22:53 
QuestionMy vote of 5! Pin
chemark2-Aug-11 20:05
Memberchemark2-Aug-11 20:05 
QuestionFileByteStream.Close() throws an exception Pin
SerLor28-Jul-11 5:05
MemberSerLor28-Jul-11 5:05 
AnswerRe: FileByteStream.Close() throws an exception Pin
Dimitris Papadimitriou28-Jul-11 5:55
MemberDimitris Papadimitriou28-Jul-11 5:55 
GeneralRe: FileByteStream.Close() throws an exception Pin
SerLor28-Jul-11 22:57
MemberSerLor28-Jul-11 22:57 
GeneralRe: FileByteStream.Close() throws an exception Pin
kiquenet.com4-Nov-11 6:18
professionalkiquenet.com4-Nov-11 6:18 
QuestionDoes this work with Mtom, Streaming, basicHttpBinding, hosted on IIS ? Pin
freddie200021-Jun-11 4:16
Memberfreddie200021-Jun-11 4:16 
QuestionCompliment and question about streams Pin
gerardo_00110-Nov-10 18:46
Membergerardo_00110-Nov-10 18:46 

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.