Skip to main content

Microsoft Silverlight

Unanswered Question SL calling a WCF service which returns a streamRSS Feed

(0)

ZZZZZZZZZ
ZZZZZZZZZ

Member

Member

3 points

10 Posts

SL calling a WCF service which returns a stream

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.MyClient myclient = new TestPrototype.MyRef.MyClient();

myclient.ExecuteQueryCompleted += new EventHandler<TestPrototype.MyRef.ExecuteQueryCompletedEventArgs>(DoWork);

myclient.ExecuteQueryAsync();

 

//Do Work Function

public void DoWork(object sender, TestPrototype.MyRef.ExecuteQueryCompletedEventArgs e)

{

 // e.Result returns a byte array but the service returns a Stream

}

 

On the server side 

[OperationContract]
public Stream ExecuteQuery()

{

          // Open a FileStream object and return it

}

 

//Bindings in Web.Config

<binding name="customBinding0">

<textMessageEncoding/>

<httpTransport transferMode="StreamedResponse" maxReceivedMessageSize="67108864"/>

</binding>

 

MarkMonster
MarkMonster

Contributor

Contributor

5220 points

1,046 Posts

Re: SL calling a WCF service which returns a stream

Did you read this thread?

http://forums.silverlight.net/forums/p/119340/271557.aspx

Mark Monster - MCPD Enterprise
http://mark.mymonster.nl
Silverlight and Expression Insiders UG

Dont forget to click "Mark as Answer" on the post that helped you.

ZZZZZZZZZ
ZZZZZZZZZ

Member

Member

3 points

10 Posts

Re: SL calling a WCF service which returns a stream

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 Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft

Re: SL calling a WCF service which returns a stream

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 = bufferIdea;
            bufferIdea = buffer[j];
            buffer[j] = currenti;
        }

    }
}
Please take a look at the sample here.
Best regards,
Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

ZZZZZZZZZ
ZZZZZZZZZ

Member

Member

3 points

10 Posts

Re: SL calling a WCF service which returns a stream

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

<customBinding>

<binding name="CustomBinding_temp" >

<textMessageEncoding messageVersion="Default" writeEncoding="utf-8" />

<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />

</binding>

</customBinding>

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities