Reputation: 1794
Like what I wrote in the title: In the server side I have this method:
[OperationContract]
Stream GetStream();
It return the stream but when I get it in the client, it return the byte[]:
public System.Threading.Tasks.Task<byte[]> GetStreamAsync() {
return base.Channel.GetStreamAsync();
}
I still dont understand. Do anyone meet this error like me, or how can I streaming with this return type.
Upvotes: 0
Views: 311
Reputation: 331
Maybe you should do like this?
Add endpoint in WCF Web.config
<endpoint address="files" behaviorConfiguration="WebBehavior"
binding="webHttpBinding" bindingConfiguration="HttpStreaming"
contract="WebService.IFileService" />
<endpoint address="data" binding="basicHttpBinding" contract="WebService.MyWCFService" />
When do like this in your service .svc file
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);
}
}
And on Client side. First update your WCF service reference, and when:
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();
}
Upvotes: 1