基于TCP协议的WCF传输大文件如何出现进度条

bqlhome 2008-07-28 03:19:21
RT.
比如传输50M的文件,
我能显示个进度条 2000Kb of 5000Kb 已传输40%.
有回复了加分.
...全文
1321 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
guan651017094 2011-04-19
  • 打赏
  • 举报
回复
我这样写之后,为什么会报错呢,在客户端 “套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的” MyFileInfo fileInfo = manager.DownloadFile(new DownloadFileRequest(remoteFileName));
lhzyn 2008-07-29
  • 打赏
  • 举报
回复
不建议用WCF做文件传输
参考以下代码(VS2008下测试通过)
Service端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
namespace FSDownloadService
{
[MessageContract]
public class MyFileInfo
{
[MessageHeader]
public string FileName;
[MessageHeader]
public long FileSize;
[MessageBodyMember]
public Stream Stream;
public MyFileInfo() { }
public MyFileInfo(Stream stream, string fileName, long fileSize)
{
this.Stream = stream;
this.FileSize = fileSize;
this.FileName = fileName;
}
}

[MessageContract]
public class DownloadFileRequest
{
[MessageBodyMember]
public readonly string FileName;
public DownloadFileRequest() { }
public DownloadFileRequest(string fileName)
{
this.FileName = fileName;
}
}
[ServiceContract]
public interface IFileManager
{
[OperationContract]
MyFileInfo DownloadFile(DownloadFileRequest request);
}
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyService : IFileManager
{
public MyFileInfo DownloadFile(DownloadFileRequest request)
{
FileInfo fi = new FileInfo(request.FileName);
MyFileInfo result = new MyFileInfo(File.OpenRead(request.FileName), request.FileName, fi.Length);
return result;
}
}

public class MyHost
{
static ServiceHost host = null;
public static void Open()
{
string baseAddress = "net.tcp://localhost:2008/FileService";
host = new ServiceHost(typeof(MyService), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(IFileManager), GetTcpBinding(), "");
host.Open();
}
public static void Close()
{
if (host != null && host.State == CommunicationState.Opened)
{
host.Close();
}
host = null;
}
public static Binding GetTcpBinding()
{
NetTcpBinding binding = new NetTcpBinding();
binding.TransferMode = TransferMode.Streamed;
binding.MaxReceivedMessageSize = int.MaxValue;
return binding;
}
}
}

Host到Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FSDownloadService
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Press <ENTER> to start the service.");
System.Console.ReadLine();
MyHost.Open();
System.Console.WriteLine("The service is started.");
System.Console.WriteLine("Press <ENTER> to stop the service.");
System.Console.ReadLine();
MyHost.Close();
}
}
}

Client端代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using FSDownloadService;
namespace FSDownloadClient
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Press <ENTER> to start the download.");
System.Console.ReadLine();
string baseAddress = "net.tcp://localhost:2008/FileService";
ChannelFactory<IFileManager> factory = new ChannelFactory<IFileManager>(MyHost.GetTcpBinding(), new EndpointAddress(baseAddress));
IFileManager manager = factory.CreateChannel();
string remoteFileName = "11.rar";
MyFileInfo fileInfo = manager.DownloadFile(new DownloadFileRequest(remoteFileName));
FileStream fs = File.Create("Client_" + fileInfo.FileName);
int bytesRead = 0;
long totalBytesRead = 0;
byte[] buffer = new byte[10000];
do
{
bytesRead = fileInfo.Stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
double currentProgress = ((double)totalBytesRead) / fileInfo.FileSize;
Console.WriteLine("Read so far {0:.00}% of the file", currentProgress * 100);
} while (bytesRead > 0);
((IClientChannel)manager).Close();
factory.Close();
}
}
}

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧