110,545
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
}
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();
}
}
}
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();
}
}
}