Skip to main content
Home Forums Silverlight Programming Accessing Web Services with Silverlight SL calling a WCF service which returns a stream
4 replies. Latest Post by ZZZZZZZZZ on November 6, 2009.
(0)
ZZZZZZZZZ
Member
3 points
10 Posts
10-29-2009 3:55 PM |
Hi,
I tried calling a WCF service from SL client which opens a huge file(1GB) using FileStream and returns it. (pasted the code below)
It returns a byte array in e.Result after completing the async call when i expect a stream.
When i execute this, it returns the whole file at once to the client in e.Result (takes some time to return to the client).
I removed the transferMode option in web.config and still i achieve the same result.
Looks like the streaming option is not working and the service is buffering the whole file before sending it to the client.
Any pointers on what is going wrong and how do i access the stream object and stream the contents in the file when they are
available on the wire insted of waiting for the whole file content to be buffered.
On the Client side
MyRef.My
myclient.ExecuteQueryCompleted += new EventHandler<TestPrototype.MyRef.ExecuteQueryCompletedEventArgs>(DoWork);
myclient.ExecuteQueryAsync();
//Do Work Function
public
{
// e.Result returns a byte array but the service returns a Stream
}
On the server side
// Open a FileStream object and return it
//Bindings in Web.Config
<
</
MarkMonster
Contributor
5220 points
1,046 Posts
10-29-2009 4:00 PM |
Did you read this thread? http://forums.silverlight.net/forums/p/119340/271557.aspx
10-29-2009 4:03 PM |
Hi Mark,
I read the thread before posting this.
It has pointers to alternate scenarios like using a duplex WCF service and WCF workflows but couldnt find any answer for this particular scenario. I am not preferring using sockets or duplex services to push data to client because my applications is not meant for polling the server. It needs a simple stream(like reading a file) and should be notified when the stream ends.
Please let me know if i am missing something
Jonathan...
All-Star
24939 points
2,425 Posts
11-05-2009 9:54 PM |
Hi ZZZZZZZZ,
basicHttpBinding has exposed "TransferMode". Please set it to "Streamed". Then do it like this.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface IStreamingSample { [OperationContract] Stream GetStream(string data); [OperationContract] bool UploadStream(Stream stream); [OperationContract] Stream EchoStream(Stream stream); [OperationContract] Stream GetReversedStream(); }
class ReverseStream : Stream { FileStream inStream; internal ReverseStream(string filePath) { //Opens the file and places a StreamReader around it. inStream = File.OpenRead(filePath); } // Other methods removed for brevity. public override int Read(byte[] buffer, int offset, int count) { int countRead=inStream.Read(buffer, offset,count); ReverseBuffer(buffer, offset, countRead); return countRead; } public override void Close() { inStream.Close(); base.Close(); } protected override void Dispose(bool disposing) { inStream.Dispose(); base.Dispose(disposing); } void ReverseBuffer(byte[] buffer, int offset, int count) { int i, j; for (i = offset, j = offset + count - 1; i < j; i++, j--) { byte currenti = buffer; buffer = buffer[j]; buffer[j] = currenti; } } }
Please take a look at the sample here.
Best regards,
Jonathan
11-06-2009 8:51 PM |
Hi Jonathan,
Thanks for the reply.
I set the TransferMode to streamed in web.config for the wcf service.
But for the silverlight client to use the WCF proxy, I define the endpoint and the binding configuration in ServiceReferences.ClientConfig.
In its the binding section it doesnt have the option of TransferMode for basicHttpBinding.
So although the service sends a stream object, i recieve it only after its completely buffered as a byte array in the silverlight client.
Here is the binding defined in ServiceReferences.ClientConfig