tags:

views:

297

answers:

1

Hi, I was looking at this thread: http://stackoverflow.com/questions/1935040/how-to-handle-large-file-uploads-via-wcf

I need to have a web service hosted at my provider where i need to upload and download files to. We are talking videos from 1Mb to 100Mb hence the streaming approach.

I cant get it to work, i declared an Interface:

[ServiceContract]
    public interface IFileTransferService
    {

        [OperationContract]
        void UploadFile(Stream stream);
    }

and all is fine, i implement it like this:

 public string FileName = "test";

        public void UploadFile(Stream stream)
        {
            try
            {
                FileStream outStream = File.Open(FileName, FileMode.Create, FileAccess.Write);
                const int bufferLength = 4096;
                byte[] buffer = new byte[bufferLength];
                int count = 0;
                while((count = stream.Read(buffer, 0, bufferLength)) > 0)
                {
                    //progress
                    outStream.Write(buffer, 0, count);
                }
                outStream.Close();
                stream.Close();
                //saved
            }
            catch(Exception ex)
            {
                throw new Exception("error: "+ex.Message);
            }
        }

Still no problem, its published to my webserver out on the interweb. So far so good.

Now i make a reference to it and will pass it a FileStream, but the argument is now a byte[] - why is that and how do i get it the proper way for streaming?

Edit My binding look like this:

 <bindings>
      <basicHttpBinding>
        <binding name="StreamingFileTransferServicesBinding"
                 transferMode="StreamedRequest"
                 maxBufferSize="65536"
                 maxReceivedMessageSize="204003200"  />
      </basicHttpBinding>
    </bindings>

I can consume it without problems, and get no errors - other than my input parameter has changed from a stream to a byte[]

Edit 2 Solved! I was a bit fast when i made the reference on my client. By accident i made a "web reference" instead of a "service reference". That solved the byte[] / stream issue

+2  A: 

Is this included as a Service Reference in your project?

I usually have my streaming service with slightly more complex objects, for example, instead of this:

[OperationContract]
void UploadFile(Stream stream);

try

[OperationContract]
void UploadFile(FileDTO stream);

[MessageContract]
public class FileDTO : IDisposable
{
    [MessageBodyMember]
    public Stream FileStream { get; set; }

    [MessageHeader]
    public String FileLabel { get; set; }
}

The call on your service reference client would look like this:

client.UploadFile(fileLabel, fileStream);

with the parameters being String, and Stream.

Ben Farmer
This doesnt change anything, the stream of the "composite" object is still byte[] when im on my client
H4mm3rHead
Read my edit 2, and thx a bunch, overlooked your first line the first time i read your post :-)
H4mm3rHead