This is the second part of a multi-part article about implementing large file upload for ASP.Net In my previous post I explained the Silverlight bit and this part will be about the WCF backend.
Curious enough, the WCF backend is actually extremely simple. The are only 3 methods that are exposed as a webservice. There 3 methods cover the 3 phases of the file upload process I explained in the previous post. But before I show the service I need to explain some thing else first.
In summary:
- The client request an upload token.
- The client sends the binary file in small blocks using the token.
- After the file is complete, the client confirms the upload.
We took care of those steps with the Silverlight client. On the server side, we need to respond to those steps.
To maintain a record of all the uploads that are in progress, we need to maintain some sort of ‘memory’. When an upload starts, a new UploadSession object is created. The UploadSession has a couple of properties:
- The Filename of the file that is being submitted,
- The ID or token of the session,
- The State that the upload is currently in (Created, Streaming, Completed or Failed), and finally:
- The binary Data of the transmitted file.
UploadManager class
To access a session, a singleton class is implemented.It has 3 methods: CreateSession and GetSession, and finally GetUpload():
CreateSession will create a new session, GetSession retrieves a session. Both are declared internal because they are used by the WCF service (we will get to that in a minute). Third, the public GetUpload() is similar to GetSession but is used to retrieve a completed transfer.
UploadSessions
The UploadSessions class is a helper collection class that is used to store the current uploads. In a first version I was keeping all the uploads in a collection that had Timer events to purge old uploads, but later I decided to use the HttpRuntime.Cache. Here’s the collection class:

In essence, it provides strongly-typed access to the cache using the indexer operator. These getter and setter forwards all calls to the HttpRuntime Cache. If you didn't know, this cache is exactly the same class as the HttpContext.Current.Cache, only shorter.
I set the sliding expiration to 5 minutes. This means that the session will be removed is no block is received within 5 minutes, or when the file is not ‘picked up’ by the consuming client after 5 minutes of the upload. This should be sufficient in most cases.
Also, I use the priority to CacheItemPriority.NotRemovable so the server does not expire the session within the timeout period is it low on resources.
(I just noticed that the Remove method is empty…)
Calling the UploadManager from WCF
The WCF service itself is extremely simple. I just forwards all methods to the UploadManager:

Last, but not least
The last step for the service side is UploadSession class itself:
Again, no rocket science. The UploadSession class is just a wrapper class for a MemoryStream. I intend to expand this class so it flushes the buffer to a temp-file if the file becomes too large, but for now it works perfectly.
The reason I am using a in-memory buffer is a) memory is cheap and fast and b) I’m using the uploaded data in combination with Linq. And Linq stored binary data as a Binary() class, which is just a wrapper class for a byte[]. Therefore it has no use in streaming the file to disk first, and then completely loading it back into memory. The disk buffer approach is only handy when lots of file are uploaded at the same time. If a lot of files are uploaded simultaneously, the total memory usage would have been lower during the time of the uploads, at the cost of requiring temporary files and a lot of disk I/O.
Hosting the Service
Now, to host this WCF service, a basic endpoint must be configured in the web.config of the hosting application:
This configuration was generated automatically when I added the service in my project, so it is completely standard. No magic here. No streaming, no large buffers, nothing fancy.
The Silverlight client needs the Url of the WCF service, but I think that was already covered in the previous post.
Wrapping it all up
This is it for the WCF side of it all. As I mentioned before, we are not finished yet. We still need a way to host the Silverlight application, and tell Asp.net that we have received a file. After that we need to pickup the file from the UploadManager. I’ll be showing you a custom server control that does all three of these steps in a simple, reusable Asp.Net control. But that topic will have to wait until the next episode.
Happy uploading!
Currently rated 5.0 by 3 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5