Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
已取消WCF REST下载_Wcf_Rest - Fatal编程技术网

已取消WCF REST下载

已取消WCF REST下载,wcf,rest,Wcf,Rest,我有一个REST服务,它通过流作为返回类型返回大文件。然而,我需要一种方法来知道有多少文件被传输,即使下载被客户端取消。完成这样的事情最好的方法是什么 到目前为止,我已经得出以下结论: public Message GetFile(string path) { UsageLog log = new UsageLog(); return WebOperationContext.Current.CreateStreamResponse((st

我有一个REST服务,它通过流作为返回类型返回大文件。然而,我需要一种方法来知道有多少文件被传输,即使下载被客户端取消。完成这样的事情最好的方法是什么

到目前为止,我已经得出以下结论:

        public Message GetFile(string path)
    {
        UsageLog log = new UsageLog();

        return WebOperationContext.Current.CreateStreamResponse((stream) =>
        {
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[_BufferSize];
                int bytesRead = 0;
                long totalBytesRead = 0;

                while (true)
                {
                    bytesRead = fs.Read(buffer, 0, _BufferSize);

                    if (bytesRead == 0)
                    {
                        break;
                    }

                    try
                    {
                        stream.Write(buffer, 0, bytesRead);
                    }
                    catch (CommunicationException ex)
                    {
                        break;
                    }

                    totalBytesRead += bytesRead;
                }

                log.TransferCompletedUtc = DateTime.UtcNow;
                log.BytesTransferred = totalBytesRead;
            }
        },
        "application/octet-stream");
    }

我很想听听其他解决方案来完成类似的任务。

您的客户是如何访问该服务的?你能展示一些代码吗?目前我正在用一个用.NET编写的演示客户端测试它,该客户端使用HttpWebRequest和HttpWebResponse来执行下载,但这并不重要,因为REST服务是为任何类型的客户端设计的。