开发者

How to stream an XElement/XDocument with WCF?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜