将视频从WCF服务器流式传输到UWP客户端

时间:2017-07-27 09:38:03

标签: c# .net wcf uwp streaming

就像我在标题中写的那样: 在服务器端,我有这个方法:

[OperationContract]
Stream GetStream();

它返回流但是当我在客户端获取它时,它返回byte []:

 public System.Threading.Tasks.Task<byte[]> GetStreamAsync() {
            return base.Channel.GetStreamAsync();
        }

我还是不明白。有没有人像我一样遇到这个错误,或者我如何使用这种返回类型进行流式传输。

1 个答案:

答案 0 :(得分:1)

也许你应该这样做?

在WCF Web.config中添加端点

<endpoint address="files" behaviorConfiguration="WebBehavior"
      binding="webHttpBinding" bindingConfiguration="HttpStreaming"
      contract="WebService.IFileService" />
<endpoint address="data" binding="basicHttpBinding" contract="WebService.MyWCFService" />

在您的服务.svc文件

中这样做的时候
namespace WebService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MyWCFService : IFileService
    {
        public Stream DownloadFile()
        {
            var filePath = "test.txt";

            if (string.IsNullOrEmpty(filePath))
                throw new FileNotFoundException("File not found");

            return File.OpenRead(filePath);
    }
}

namespace WebService
{
    [ServiceContract]
    public interface IFileService
    {
        [WebGet]
        Stream DownloadFile(string FileId);
    }
}

在客户端。首先更新您的WCF服务引用,以及何时:

public async Task DownloadFile(string FileId)
    {
        string serverAddress = "http...../MyWCFService.svc";

        string filename = "test.txt";

        StorageFolder folder = KnownFolders.PicturesLibrary;
        var file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

        BackgroundDownloader downloader = new BackgroundDownloader();
        DownloadOperation download = downloader.CreateDownload(new Uri($"{serverAddress}/files/DownloadFile?FileId={FileId}"), file);

        await download.StartAsync();
    }