Click here to Skip to main content
6,929,467 members and growing! (14,698 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Communication Foundation » Utilities     Intermediate License: The Code Project Open License (CPOL)

WCF Large File Download Upload Service

By HarishBhattbhatt

A WCF document mangement service for downloading and uploading large files, hosted on a Windows Service.
C#1.0, C#2.0, C#3.0.NET3.0, .NET3.5, WPF, Dev
Revision:4 (See All)
Posted:26 Jun 2009
Views:11,937
Bookmarked:21 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 3.05 Rating: 2.74 out of 5
6 votes, 46.2%
1
2 votes, 15.4%
2

3
1 vote, 7.7%
4
4 votes, 30.8%
5

Introduction

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.

[ServiceContract]
public interface IDocumentLibraryService
{
    [OperationContract]
    void UploadDocument(string fPath, byte[] data);
 
    [OperationContract]
    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, 
                                     FileAccess.Write);
      fs.Write(data, 0, data.Length);
      fs.Close();
   }   
   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.
    StartWCFService();
}

void StartWCFService()
{
    try
    {
        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);
        //ntcp.OpenTimeout
        //ntcp.CloseTimeout
 
        svh = new ServiceHost(typeof(DocumentLibraryService));
        ((ServiceBehaviorAttribute)
           svh.Description.Behaviors[0]).MaxItemsInObjectGraph = 2147483647;
        svh.AddServiceEndpoint(
                    typeof(IDocumentLibraryService.IDocumentLibraryService),
                    ntcp,
                    "net.tcp://localhost:8000");
        svh.Open();
    }
    catch (Exception e)
    {
        Trace.WriteLine(e.Message);
    }
}

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, 
                                        "net.tcp://localhost:8000");
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.

Conclusion

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.

License

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

About the Author

HarishBhattbhatt


Member

Occupation: Team Leader
Company: Phoenix Tech Labs
Location: India India

Other popular Windows Communication Foundation articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberMember 317768414:58 17 Jan '10  
GeneralMy vote of 1 PinmemberJO Hyeong-Ryeol22:29 8 Dec '09  
GeneralMy vote of 1 PinmemberNightDorian5:16 6 Aug '09  
GeneralLarge video files upload (file size over 10 gb) PinmemberSharmistha Sen4:22 16 Jul '09  
GeneralMy vote of 1 PinmemberAnderson Imes6:26 7 Jul '09  
GeneralWCF Streaming PinmemberAnderson Imes6:25 7 Jul '09  
GeneralMy vote of 1 PinmemberJaredFox11:52 30 Jun '09  
GeneralMy vote of 2 PinmemberWillemToerien3:54 29 Jun '09  
GeneralNot a good idea PinmemberWillemToerien3:53 29 Jun '09  
GeneralMy vote of 2 Pinmemberrcardare3:44 27 Jun '09  
GeneralMy vote of 1 Pinmember_FleX5:01 26 Jun '09  
GeneralRe: My vote of 1 PinmemberHarishBhattbhatt5:16 26 Jun '09  
Hi _Flex,

I am really happy that you have given some comment to article.
can you please suggest where are the loopholes in the method
it will help everyone here...

As everybody is learning...

Thanks again
GeneralRe: My vote of 1 Pinmemberdchrno5:52 26 Jun '09  
GeneralRe: My vote of 1 PinmemberHarishBhattbhatt22:35 26 Jun '09  
GeneralRe: My vote of 1 PinmemberDaniel Cazzulino [XML MVP]11:26 21 Feb '10  
GeneralRe: My vote of 1 PinmemberDruuler8:45 26 Jun '09  
GeneralRe: My vote of 1 PinmemberGuillaume Leparmentier20:44 26 Jun '09  
Generalquestion PinmemberSavara4:18 26 Jun '09  
GeneralRe: question PinmemberHarishBhattbhatt22:42 26 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 26 Jun 2009
Editor: Smitha Vijayan
Copyright 2009 by HarishBhattbhatt
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project