Resmi Satish

Resmi Satish

  • 2k
  • 110
  • 67.3k

Download file using WCF

Jun 26 2014 8:11 AM

I have to create wcf service for download file. I have
refer :

However I am getting error for
  [MessageBodyMember(Order = 1)]
 public System.IO.Stream FileByteStream;

When I am including this and consuming my service using a website, I am getting the error The underlying connection was closed: The connection was closed unexpectedly.

My IService.cs

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

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

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

    public void Dispose()
        if (FileByteStream != null)
            FileByteStream = null;

My service.cs has

 public RemoteFileInfo DownloadFile(DownloadRequest request)
        RemoteFileInfo result = new RemoteFileInfo();

        string filePath = System.IO.Path.Combine(@"D:\DownLoad", request.FileName);
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

        // check if exists
        if (!fileInfo.Exists)
            throw new System.IO.FileNotFoundException("File not found",

        ////// open stream
        System.IO.FileStream stream = new System.IO.FileStream(filePath,
                  System.IO.FileMode.Open, System.IO.FileAccess.Read);

        ////// return result
       result.FileName = request.FileName;
       result.Length = fileInfo.Length;
       result.FileByteStream = stream;
        return result;


//Website page

            CarService.IService clientDownload = new CarService.ServiceClient();
            CarService.DownloadRequest requestData = new CarService.DownloadRequest();

          CarService.RemoteFileInfo fileInfo=new CarService.RemoteFileInfo();

            requestData.FileName = "Test.txt";

            fileInfo = clientDownload.DownloadFile(requestData);

I have add all the configuration values as per the link I have referred.

Can we use "System.IO.Stream" as  [MessageBodyMember(Order = 1)]?

Please help me to resolve the issue.

Answers (1)