|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article examines the implementation of upload and download functionality with progress indication (progress bar feature) using the Windows Communication Foundation. Here is a list of what you need:
The sample code consists of three projects bundled in a solution. A brief description of these projects follows. The attached sample code is available in C# and VB.NET (conversion to VB.NET was made by Lee Galloway of Project Time and Cost). FileServiceThis is the main server project. The Server ContractThe File Server project includes FileTransferServiceContract.cs file, which contains the
CS[ServiceContract()]
public interface IFileTransferService
{
[OperationContract()]
void UploadFile(RemoteFileInfo request);
[OperationContract]
RemoteFileInfo DownloadFile(DownloadRequest request);
}
public class RemoteFileInfo
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
}
VB<ServiceContract()> _
<servicecontract() />Public Interface IFileTransferService
<OperationContract()> _
Sub UploadFile(ByVal request As RemoteFileInfo)
<OperationContract()> _
Function DownloadFile(ByVal request As DownloadRequest) As RemoteFileInfo
End Interface
<MessageContract()> _
Public Class RemoteFileInfo
Implements IDisposable
<OperationContract()> _
Public FileName As String
<OperationContract()> _
Public Length As Long
<MessageBodyMember(Order:=1)> _
Public FileByteStream As System.IO.Stream
End Class
The Server ImplementationFile Server also includes the FileTransferService.cs code file which contains the implementation of the contract, i.e. the actual code that does all the work. Apparently the included class implements the Note here that since actual downloading of the file starts after the execution of the ConsoleHost
CSstatic void Main(string[] args)
{
ServiceHost myServiceHost = new ServiceHost
(typeof(FileService.FileTransferService));
myServiceHost.Open();
Console.WriteLine("This is the SERVER console");
Console.WriteLine("Service Started!");
foreach (Uri address in myServiceHost.BaseAddresses)
Console.WriteLine("Listening on " + address.ToString());
Console.WriteLine("Click any key to close...");
Console.ReadKey();
myServiceHost.Close();
}
VBPublic Shared Sub Main()
Dim myServiceHost As New ServiceHost(_
GetType(FileService.FileTransferService))
myServiceHost.Open()
Console.WriteLine("This is the SERVER console")
Console.WriteLine("Service Started!")
For Each address As Uri In myServiceHost.BaseAddresses
Console.WriteLine("Listening on " + address.ToString())
Next
Console.WriteLine("Click any key to close...")
Console.ReadKey()
myServiceHost.Close()
End Sub
The configuration of XML<binding name ="FileTransferServicesBinding"
transferMode="Streamed"
messageEncoding="Mtom"
maxReceivedMessageSize="10067108864" >
</binding>
ClientThe Again, if you have worked with streams before, you will notice that things are pretty simple in the TestForm file except for one small part, which is also the difference in implementing the progress indication when uploading rather than when downloading. When downloading, the client has control of the procedure. You can see in TestForm.cs that downloading is implemented using a loop that reads the server stream piece-by-piece. So, the client knows what part of the server stream is read and how many more remain. When uploading, that loop resides on the server. In order for the client to know how many bytes the server read, it uses the CSpublic override
int Read(byte[] buffer, int offset,
int count)
{
int result = file.Read(buffer, offset, count);
bytesRead += result;
if (ProgressChanged != null)
ProgressChanged(this, new ProgressChangedEventArgs(bytesRead, length));
return result;
}
VBPublic Overloads Overrides Function Read(ByVal buffer As Byte(), _
ByVal offset As Integer, ByVal count As Integer) As Integer
Dim result As Integer = file.Read(buffer, offset, count)
bytesRead += result
RaiseEvent ProgressChanged(Me, New ProgressChangedEventArgs(_
bytesRead, m_length))
Return result
End Function
History
| |||||||||||||||||||||