简体   繁体   English

如何使用WCF流式传输XElement / XDocument?

[英]How to stream an XElement/XDocument with WCF?

I have the following method signature. 我有以下方法签名。 I cannot change it (ie I cannot change the return type). 我不能更改它(即我不能更改返回类型)。

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? 如何使用WCF流传输XElement / XDocument?

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; 注意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. 希望 WCF能够对流进行正确的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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