视频流
问题描述:
就像我在标题中写道: 在服务器端我有这样的方法:视频流
[OperationContract]
Stream GetStream();
它返回流,但是当我得到它在客户端,它返回字节[]:
public System.Threading.Tasks.Task<byte[]> GetStreamAsync() {
return base.Channel.GetStreamAsync();
}
我还是不明白。任何人都会遇到像我这样的错误,或者我如何使用这种返回类型进行流式传输。
答
也许你应该这样做?在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();
}
我没有看到它在我的WCF实例:( –
尝试使用'''[WebInvoke]'''属性,而不是'''[OperationContract的] '''或者你的客户端没有看到任何WCF方法? – ashchuk
它自动改变我在客户端的返回类型:( –