2017-07-27 106 views
0

就像我在標題中寫道: 在服務器端我有這樣的方法:視頻流

[OperationContract] 
Stream GetStream(); 

它返回流,但是當我得到它在客戶端,它返回字節[]:

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

我還是不明白。任何人都會遇到像我這樣的錯誤,或者我如何使用這種返回類型進行流式傳輸。

回答

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(); 
    } 
+0

我沒有看到它在我的WCF實例:( –

+0

嘗試使用'''[WebInvoke]'''屬性,而不是'''[OperationContract的] '''或者你的客戶端沒有看到任何WCF方法? – ashchuk

+0

它自動改變我在客戶端的返回類型:( –