Resmi Satish

Resmi Satish

  • NA
  • 110
  • 75.3k

Download file using WCF

Jun 26 2014 8:11 AM
Hi

I have to create wcf service for download file. I have
refer :http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP

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

   [MessageContract]
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.Close();
            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",
                                                      request.FileName);

        ////// 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)