簡體   English   中英

在WCF和Windows Service中下載大文件

[英]Large File download in WCF and Windows Service

我一直在創建一項新服務,以將大文件下載到客戶端。 我想在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請幫助我。

我在您的服務中發現了非常不錯的代碼:

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;         
} 

不要關閉服務上的流。 關閉流是客戶的責任。

這篇文章。 是您要找的東西嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM