In WCF service I fill Stream according to this question like :
result.Stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(result.Stream);
foreach (string fileN in zipFiles)
{
byte[] fileBytes = File.ReadAllBytes(fileN);
writer.Write(BitConverter.GetBytes(fileBytes.Length), 0, 4);
writer.Write(fileBytes, 0, fileBytes.Length);
}
writer.Flush();
return result;
before this I was returning stream by this and everything works in service and client side:
result.Stream = new MemoryStream(File.ReadAllBytes(fileN));
Stream be MessageBodyMember
but nut now changed it to save all file in one stream.
and Test method in client side:
ExportClient export = new ExportClient("exportEndPoint");
ExportResult_C result = export.Export(source);
result.Stream.Position = 0;
//result.Stream.SaveToFile("d:\\kkk.log");
BinaryReader reader = new BinaryReader(result.Stream, System.Text.Encoding.UTF8);
string pathToSave = string.Empty;
while (result.Stream.Position < result.Stream.Length)
{
int size = reader.ReadInt32();
byte[] data = reader.ReadBytes(size);
pathToSave = "D:\\test\\" + new Random().Next(0, 2564586).ToString() + ".zip";
File.WriteAllBytes(pathToSave, data);
}
the endpoint address:
<endpoint address="net.tcp://localhost:2082/Exchange/Export.svc" binding="netTcpBinding" bindingConfiguration="largeSizeStreamTcp"
contract="xxx" name="exportEndPoint"/>
and binding configuration:
<netTcpBinding>
<binding openTimeout="00:00:03" maxReceivedMessageSize="2000000000" transferMode="Streamed" maxBufferSize="2000000000" >
<readerQuotas maxDepth="32" maxArrayLength="2000000000" maxStringContentLength="2000000000" />
<security mode="None" />
</binding>
<binding name="largeSizeStreamTcp" transferMode="Streamed" receiveTimeout="00:30:00" sendTimeout="00:30:00" openTimeout="00:00:01" maxReceivedMessageSize="2000000000" maxBufferSize="2000000000" >
<readerQuotas maxDepth="32" maxArrayLength="2000000000" maxStringContentLength="2000000000" />
<security mode="None" />
</binding>
</netTcpBinding>
<netNamedPipeBinding>
I believe endpoint and binding is correct as I was able to return one file stream and save it back but now there is no prob in service side but when it will get from client side, Stream got lost it's content,length,position.
this is really drive me up the wall!!!
does anyone know why this is occurred(in client side) ?
Wooow, Finally I get success to implement our scenario properly, going to put the answer here maybe someone wants to use the solution to returns multiple files in one stream.
there are several sample to return multiple files but all of them are returning byte array, I prefer to return stream because of many reason, the important thing is streams will perform better for large files since not all of it needs to be read into memory at one time and backward compatibility for my case.
So at the first step I have created a serializable
DataContract
to hold the name of file and it's content byte.at the second step a list of
ExportSourceFile_C
will be created like:at the third step the mentioned list should be serialized to
result.Stream
like:in client side this is enough deserialize the stream to list of
ExportSourceFiles_C
so the last step in client side should be something like this:everything works like a charm without any problem.
enjoy.