【问题标题】:Large File download in WCF and Windows ServiceWCF 和 Windows 服务中的大文件下载
【发布时间】:2010-08-27 11:50:48
【问题描述】:

我一直在创建一个新服务来将大文件下载到客户端。我想在 Windows 服务中托管服务。 在服务中我正在写作:

 public class FileTransferService : IFileTransferService
    {
        private string ConfigPath
        {
            get
            {
                return ConfigurationSettings.AppSettings["DownloadPath"];
            }
        }
        private FileStream GetFileStream(string file)
        {

            string filePath = Path.Combine(this.ConfigPath, file);
            FileInfo fileInfo = new FileInfo(filePath);

            if (!fileInfo.Exists)
                throw new FileNotFoundException("File not found", file);

            return new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        }

        public RemoteFileInfo DownloadFile(DownloadRequest request)
        {
            FileStream stream = this.GetFileStream(request.FileName);

            RemoteFileInfo result = new RemoteFileInfo();
            result.FileName = request.FileName;
            result.Length = stream.Length;
            result.FileByteStream = stream;
            return result;
        }
    }

界面如下:

 [ServiceContract]
    public interface IFileTransferService
    {
        [OperationContract]
        RemoteFileInfo DownloadFile(DownloadRequest request);
    }
    [DataContract]
    public class DownloadRequest
    {
        [DataMember]
        public string FileName;
    }

    [DataContract]
    public class RemoteFileInfo : IDisposable
    {
        [DataMember]
        public string FileName;

        [DataMember]
        public long Length;

        [DataMember]
        public System.IO.Stream FileByteStream;

        public void Dispose()
        {
            if (FileByteStream != null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }
        }
    }

当我调用该服务时,它显示“基础连接已关闭”。 你可以得到实施 http://cid-bafa39a62a57009c.office.live.com/self.aspx/.Public/MicaUpdaterService.zip 请帮帮我。

【问题讨论】:

    标签: c# .net windows wcf web-services


    【解决方案1】:

    我在您的服务中发现了非常好的代码:

    try
    {
      host.Open();
    }
    catch {}
    

    这是最糟糕的反模式之一!立即将此代码替换为正确的错误处理和日志记录。

    我没有测试您的服务,只是通过查看配置和您的代码,我建议它永远不会工作,因为它不符合通过 HTTP 流式传输的要求。当你想通过 HTTP 方法进行流式传输时,必须只返回 Stream 类型的单个主体成员。您的方法改为返回数据合同。使用这个版本:

    [ServiceContract]     
    public interface IFileTransferService     
    {     
        [OperationContract]     
        DownloadFileResponse DownloadFile(DownloadFileRequest request);     
    }     
    
    [MessageContract]     
    public class DownloadFileRequest     
    {     
        [MessageBodyMember]     
        public string FileName;     
    }     
    
    [MessageContract]     
    public class DownloadFileResponse    
    {     
        [MessageHeader]     
        public string FileName;     
    
        [MessageHeader]     
        public long Length;     
    
        [MessageBodyMember]     
        public System.IO.Stream FileByteStream;         
    } 
    

    不要关闭服务上的流。关闭流是客户的责任。

    【讨论】:

    • 哇哦.. 它奏效了。只是另一个问题,你能告诉我如何增加消息的大小。它告诉我增加 MaxReceivedMessageSize。
    • 必须在客户端设置maxReceiveMessageSize。
    • 哇。我刚刚将客户端中的传输模式更改为流式传输,它神奇地工作。 transferMode="Streamed" 非常感谢您的帮助。干杯。
    【解决方案2】:

    查看this 文章。是你要找的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多