Desde el servicio de transmisión de Wcf directamente al disco

I have got a wcf service that sends me streams (large ones usually). As the client application my role is to get a stream over WCF and save it to disk. I've written some code but it seems like first getting the stream into ram and then write it to disk from ram. I want to safely get the stream and writing it directly to disk while not filling the ram with huge files. What is the good way of doing this? Here is what I did until now:

Stream sourceStream = SsClient.GetFile(FolderId, Helper.GetISession());
using (var targetStream = new FileStream(thisComputerPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
    //read from the input stream in 65000 byte chunks
    const int bufferLen = 65000;
    var buffer = new byte[bufferLen];
    int count;
    while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
        // save to output stream
        targetStream.Write(buffer, 0, count);

I hope I could explain my problem clear enough. Excuse me for my english by the way.

I don't mind using ram for buffering purposes or something like that, i just don't want it to be filled with 1-2 gb of streams each time as it would give clients computer hard times if it just has 2 gb of ram.

preguntado el 05 de mayo de 13 a las 12:05

1 Respuestas

Did you check the following posts

How to Save a Stream


Writing large stream to a file

Let us know incase of any queries on these implementations.

contestado el 23 de mayo de 17 a las 12:05

in these implementations it looks like input stream is already there. My problem is while gathering the stream from a webservice can i write it to disk. For example in second solution can I use it like: Copy(SsClient.GetFile(FolderId, Helper.GetISession()), targetStream, progressCallback); ? The return type of my GetFile function is stream btw. - Tolga Evcimen

You can get a stream to a file and then copy the WCF streamed data into that file. You can use as you have posted here. Also, there is a blog that states on how to read all the data into memory in WCF and then send them out to the client in chunks. Please check this link to see if it is viable for your requirement:… - Saravanan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.