简体   繁体   中英

WCF Return a stream before its fully written to

Is there a way to start returning a stream while still adding to it in a loop.

My goal is to return x number of records. Eg 10 records. But I dont want to have to process them and buffer them all prior to sending as it takes a second per record so thats a 10 second delay.

I would prefer that the consumer recieves the records as they are added, and can start processing them before all 10 records are done.

I was origionally requesting them one by one, and waiting until processed on both sides, before requesting the next one. But isn't this slower? As it seems to me like the server or consumer would be sat idle while the other works.. when it could be processing towards the end goal.

Both implementations wait til all 10 are done before returning.

 //public List<MyObject> GetObjects(int count)
 public Stream GetStream(int count)
    {
        var stream = new MemoryStream();

        for (int i = 0; i < count; i++)
        {
            var myObject = GetNextObject();

            if (myObject!= null)
            { 
                stream .Write(myObject);
            }
            else // break from loop
                break;
        }

        return stream;
    }

You can try using a duplex service . The server can send each object across to a callback on the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM