This is a mirror of official site: http://jasper-net.blogspot.com/

WCF Streaming: Upload/Download Files Over HTTP

| Sunday, March 13, 2011
Purpose of this article

I have tried to transfer large files over HTTP to/from WCF, but I have faced problems in that I was not able to upload files more than 45 KB in size. I have Googled over www, but I did not find any ready-to-use sample code/solution. Using the explanations over the www and MSDN, I have tried many configuration setting combinations and finally succeeded in transferring large files (I have tested up to 1GB on IE6).

I would like to share my experience so as to support efforts of others in this direction and to invite review comments from the developer community.

Explanation

To transfer large files using “WCF service + HTTP”, we can use the following types of bindings:

wsHttpBinding
basicHttpBinding

In wsHttpBinding, we can set the transfermode attribute as Buffered, but there is a disadvantage in using this approach for large files, because it needs to put the entire file in memory before uploading/downloading, A large buffer is required on both the web client and the WCF service host. However, this approach is very useful for transferring small files, securely.

In basicHTTPBinding we can use the transfermode as Streamed so that the file can be transferred in the form of chunks. We have to ensure additional security mechanisms for transferring chunks. The security mechanisms are not explained in this posting.

Implementation: WCF Service

Create a new “WCF Service” project. Create a new service with the name TransferService. Now we will see an interface file “ITransferService” and a class file TransferService.cs. ITransferService should have two methods, one for upload and one for download.

WCF Service Sample Interface Code:

[ServiceContract]
public interface ITransferService
{
    [OperationContract]
    RemoteFileInfo DownloadFile(DownloadRequest request);
 
    [OperationContract]
     void UploadFile(RemoteFileInfo request); 
}
[MessageContract]
public class DownloadRequest
{
    [MessageBodyMember]
    public string FileName;
}

[MessageContract]
public class RemoteFileInfo : IDisposable
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName;

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

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

Read more: Codeproject

Posted via email from Jasper-net

0 comments: