开发者

How to stream an XElement/XDocument with WCF?

开发者 https://www.devze.com 2023-04-09 09:30 出处:网络
I have the following method signature. I cannot change it (i.e. I cannot change the return type). 开发者_如何学JAVApublic Stream GetMusicInfo(string songId)

I have the following method signature. I cannot change it (i.e. I cannot change the return type).

开发者_如何学JAVA
public Stream GetMusicInfo(string songId)
{
    XElement data = dao.GetMusicInfo(songId);

    // how do I stream the XElement?
}

How can I stream the XElement/XDocument with WCF?


That's reasonably simple, if you don't mind actually fetching all the data in that first line:

public Stream GetMusicInfo(string songId)
{
    XElement data = dao.GetMusicInfo(songId);
    MemoryStream ms = new MemoryStream();
    data.Save(ms);
    ms.Position = 0;
    return ms;
}

In other words, just write it out in-memory, and return a stream over that in-memory representation. Note the Position = 0; call, which is necessary as otherwise the stream will be positioned at the end of the data.

I would hope that WCF would then just do the right thing with the stream.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号