我一直在创建一个新的服务来将大文件下载到客户端。我想在Windows服务中托管该服务。 在服务我写:在WCF和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 请帮助我。
Whohoo ..它的工作。 只是另一个问题,你能告诉我如何增加消息的大小。它告诉我增加MaxReceivedMessageSize。 – abhishek 2010-08-27 13:02:12
您必须在客户端上设置maxReceiveMessageSize。 – 2010-08-27 13:05:42
哇。我刚刚将客户端中的传输模式更改为流式传输,并且它的工作方式很神奇。 transferMode =“Streamed” 非常感谢您的帮助。 干杯。 – abhishek 2010-08-27 14:31:44