大文件下载在WCF和Windows服务大文件、WCF、Windows

由网友(ancestor 小祖宗)分享简介:我一直在创造一个新的服务下载大文件的客户端。我想举办服务在Windows服务。在服务我写:I have been creating a new service to download large file to client. I want to host the service in a Windows Serv...

我一直在创造一个新的服务下载大文件的客户端。我想举办服务在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.

阅读全文

相关推荐

最新文章