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,
- namespace WCFTestService
- {
- [ServiceContract(SessionMode = SessionMode.Required)]
- public interface IServiceClass
- {
- [OperationContract]
- int AddNumbers(int number1, int number2);
- [OperationContract]
- MyFileInfo DownloadFile();
- }
- [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, InstanceContextMode=InstanceContextMode.PerSession)]
- public class ServiceClass:IServiceClass
- {
- int IServiceClass.AddNumbers(int number1, int number2)
- {
- return number1 + number2;
- }
- MyFileInfo IServiceClass.DownloadFile()
- {
- string fname = @"E:/Softwares/QQ2008Spring.exe";
- Stream fs = File.OpenRead(fname);
- MyFileInfo fi = new MyFileInfo(fs, fname, fs.Length);
- return fi;
- }
- }
- [MessageContract]
- public class MyFileInfo
- {
- private string fileName;
- private long fileLength;
- [MessageBodyMember]
- public Stream fileContent;
- public MyFileInfo(Stream fs, string fileName, long fileLength)
- {
- this.fileName = fileName;
- this.fileLength = fileLength;
- this.fileContent = fs;
- }
- [MessageHeader]
- public string FileName
- {
- get { return fileName; }
- set { fileName = value; }
- }
- [MessageHeader]
- public long FileLength
- {
- get { return fileLength; }
- }
- }
- }
3. on the client side. you have to add service referenct correctly.
- TCP.ServiceClassClient client = new WCFServiceTestClient.TCP.ServiceClassClient("NetTcpBinding_IServiceClass");
- Stream tmpStream ;
- string tmpName;
- long tmpLength = client.DownloadFile(out tmpName, out tmpStream);
- textBox1.Text = tmpName;
- textBox2.Text = tmpLength.ToString();
- // save the file to client side.
- FileStream fs = File.Create(@"d:/Client_aa.exe" );
- int bytesRead = 0;
- byte[] buffer = new byte[100000];
- do
- {
- bytesRead = tmpStream.Read(buffer, 0, buffer.Length);
- fs.Write(buffer, 0, bytesRead);
- totalBytesRead += bytesRead;
- double currentProgress = ((double)totalBytesRead) / tmpLength;
- // show the transfer status based on currentProgress variable.
- } while (bytesRead > 0);
- 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.
- <netTcpBinding>
- <binding name="NetTcpBinding_IServiceClass" closeTimeout="00:01:00"
- openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
- transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
- hostNameComparisonMode="StrongWildcard" listenBacklog="10"
- maxBufferPoolSize="524288" maxBufferSize="2147483647" maxConnections="10"
- maxReceivedMessageSize="2147483647">
- <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
- maxBytesPerRead="4096" maxNameTableCharCount="16384" />
- <reliableSession ordered="true" inactivityTimeout="00:10:00"
- enabled="false" />
- <security mode="Transport">
- <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
- <message clientCredentialType="Windows" />
- </security>
- </binding>
- </netTcpBinding>