大文件下载在WCF和Windows服务 [英] Large File download in WCF and Windows Service

查看:181
本文介绍了大文件下载在WCF和Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在创造一个新的服务下载大文件的客户端。我想举办服务在Windows服务。 在服务我写:

I have been creating a new service to download large file to client. I want to host the service in a Windows Service. In service I am writing :

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

该接口是这样的:

The Interface looks like :

 [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  请帮我。

When I am calling the service it says "Underlying connection was closed." You can get the implementation http://cid-bafa39a62a57009c.office.live.com/self.aspx/.Public/MicaUpdaterService.zip Please help me.

推荐答案

我已经找到非常好的code。在您的服务:

I have found very nice code in your service:

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

这是最糟糕的反模式之一!立即更换该code正确的错​​误处理和日志记录。

This is one of the worst antipatterns! Immediately replace this code with correct error handling and logging.

我没有测试你的服务,但是仅仅通过寻找配置和你的code我认为它不会工作,因为它不符合通过HTTP流的要求。当你要流通过HTTP方法必须返回唯一的单机身部件,它的类型是流。你的方法返回的数据,而不是合同。使用此版本:

I didn't test your service but simply by looking to config and to your code I suggested that it will never work because it doesn't meet the requirements for streaming over HTTP. When you want to stream over HTTP method must return only single body member which is of type Stream. Your method returns data contract instead. Use this version:

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

不要在服务关闭流。这是客户的责任,关闭这个流。

Do not close the stream on the service. It is client's responsibility to close the stream.

这篇关于大文件下载在WCF和Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆