Transfer file with WCF TCP binding.

1. Configure your app.config to ensure you have a valid service bind and host. If you don't know how to do it, please see my previous posts about WCF.

 

2. You have to create message contract for the file you want to transfer. Here i just hard code it, in reality, you have to create more flexible application. I just want to make an example to show how to do it.

 

On the service side,

  1. namespace WCFTestService
  2. {
  3.     [ServiceContract(SessionMode = SessionMode.Required)]
  4.     public interface IServiceClass
  5.     {
  6.         [OperationContract]
  7.         int AddNumbers(int number1, int number2);
  8.         [OperationContract]
  9.         MyFileInfo DownloadFile();
  10.     }
  11.     [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, InstanceContextMode=InstanceContextMode.PerSession)]
  12.     public class ServiceClass:IServiceClass
  13.     {
  14.         int IServiceClass.AddNumbers(int number1, int number2)
  15.         {
  16.             return number1 + number2;
  17.             
  18.         }
  19.         MyFileInfo IServiceClass.DownloadFile()
  20.         {
  21.             string fname = @"E:/Softwares/QQ2008Spring.exe";
  22.             Stream fs = File.OpenRead(fname);
  23.             MyFileInfo fi = new MyFileInfo(fs, fname, fs.Length);
  24.             return fi;            
  25.         }
  26.     }
  27.     [MessageContract]
  28.     public class MyFileInfo
  29.     {
  30.         private string fileName;
  31.         private long fileLength;
  32.         [MessageBodyMember]
  33.         public Stream fileContent;
  34.         public MyFileInfo(Stream fs, string fileName, long fileLength)
  35.         {
  36.             this.fileName = fileName;
  37.             this.fileLength = fileLength;
  38.             this.fileContent = fs;
  39.         }
  40.         [MessageHeader]
  41.         public string FileName
  42.         {
  43.             get { return fileName; }
  44.             set { fileName = value; }
  45.         }
  46.         [MessageHeader]
  47.         public long FileLength
  48.         {
  49.             get { return fileLength; }
  50.         }
  51.     }
  52. }

3. on the client side. you have to add service referenct correctly.

 

  1. TCP.ServiceClassClient client = new WCFServiceTestClient.TCP.ServiceClassClient("NetTcpBinding_IServiceClass");
  2.             Stream tmpStream ;
  3.             string tmpName;
  4.             long  tmpLength = client.DownloadFile(out tmpName, out tmpStream);
  5.             textBox1.Text = tmpName;
  6.             textBox2.Text = tmpLength.ToString();
  7.             // save the file to client side.
  8.             FileStream fs = File.Create(@"d:/Client_aa.exe" );
  9.             int bytesRead = 0;
  10.             
  11.             byte[] buffer = new byte[100000];
  12.             do
  13.             {
  14.                 bytesRead = tmpStream.Read(buffer, 0, buffer.Length);
  15.                 fs.Write(buffer, 0, bytesRead);
  16.                 totalBytesRead += bytesRead;
  17.                 double currentProgress = ((double)totalBytesRead) / tmpLength;
  18.                 // show the transfer status based on currentProgress variable.
  19.             } while (bytesRead > 0);
  20.             fs.Close();

4. One thing i have to point out is that the default configuration for client to receive message is only 65535 byes. you must enlarge it based on your requirements.

  1. <netTcpBinding>
  2.                 <binding name="NetTcpBinding_IServiceClass" closeTimeout="00:01:00"
  3.                     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
  4.                     transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
  5.                     hostNameComparisonMode="StrongWildcard" listenBacklog="10"
  6.                     maxBufferPoolSize="524288" maxBufferSize="2147483647" maxConnections="10"
  7.                     maxReceivedMessageSize="2147483647">
  8.                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
  9.                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  10.                     <reliableSession ordered="true" inactivityTimeout="00:10:00"
  11.                         enabled="false" />
  12.                     <security mode="Transport">
  13.                         <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
  14.                         <message clientCredentialType="Windows" />
  15.                     </security>
  16.                 </binding>
  17.             </netTcpBinding>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值