windows WCF和Windows服务中的大文件下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3583810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Large File download in WCF and Windows Service
提问by abhishek
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 :
我一直在创建一个新服务来将大文件下载到客户端。我想在 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;
}
}
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;
}
}
}
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.zipPlease help me.
当我调用该服务时,它说“底层连接已关闭”。你可以得到实现 http://cid-bafa39a62a57009c.office.live.com/self.aspx/.Public/MicaUpdaterService.zip请帮帮我。
回答by Ladislav Mrnka
I have found very nice code in your service:
我在您的服务中发现了非常好的代码:
try
{
host.Open();
}
catch {}
This is one of the worst antipatterns! Immediately replace this code with correct error handling and logging.
这是最糟糕的反模式之一!立即用正确的错误处理和日志替换此代码。
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:
我没有测试你的服务,只是通过查看配置和你的代码,我建议它永远不会工作,因为它不满足通过 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;
}
Do not close the stream on the service. It is client's responsibility to close the stream.
不要关闭服务上的流。关闭流是客户的责任。