File Manager – A Demo of a WCF Self-Hosted Service & Client “Tester” Windows Form Application Exchanging Files

About

This project presents a simple File Manager Service and Client Application demonstration. The File Manager is a self-hosted (service host) WCF application launched and managed with a simple console interface. The client β€œtester” has a simplified GUI user interface to quickly demo and test the service (Windows Form Application).

Architecture

The demo project consists of these component topics:

  • Shared Class Library Project β€œSharedLibrary”
    • IFileManagerService (Interface for Service)
    • FileInformation (Class Describing a File Object)
  • Service Class Library Project β€œFileManagerServiceLibrary”
    • FileManagerService (Code that Implements the Service Interface)
    • application configuration (Configuration Reference for Service Host)
    • Reference to the Shared Class Library
  • WCF Service (Host) Application Project β€œFileManagerServerHost”
    • Program (Starts, Manages, Stops the Service)
    • application configuration (Configuration for Service Host)
    • Reference to the FileManagerServiceLibrary
  • Client β€œTester to Service” Windows Form Application Project β€œClient”
    • Reference to the Shared Class Library
    • Main Form GUI User Interface
    • Form Code – Processes GUI User Interface

The service interface is defined not in the service application but in a Shared Library. This library defines the interface contracts for the file manager services (ex: Get File) and is referenced by both the client and service host projects.Β  The SharedLibrary also has a class definition that defines a file object with a name and size property.

The FileManagerServiceLibrary implements the File Manager service and contracts as defined in the SharedLibrary. The FileManagerServerHost is a simple console application that is responsible for starting the File Manager service, hosting, and managing the service (self-hosted).

The service behaviors were designed to allow multiple threads (concurrency) while each client call to the service spins up a new service instance (PerCall). Although this project is not meant to demo concurrency behaviors, I would recommend referencing β€œThe Money Pot Problem” [link] to read my discussion on service behaviors and concurrency.

A client β€œtester” windows form application tests the service and provides output to the user in a simple GUI.

In addition, there will be a short discussion of streaming and chunking techniques used by the project to download and upload files. Also, the client application implements asynchronous programming techniques in creating separate threads while allowing for cancellation.

Shared Class Library

A Class Library project (SharedLibrary) was added to my Visual Studio solution. This library is shared amongst the client and the service. The code is available on GitHub [here].

IFileManagerService (Interface for Service)

The ServiceContract for the File Manager service contains five operation contracts. The code is available on GitHub [here].

  • List<FileInformation> GetFiles();
    • Returns a list of files in the server directory
  • Stream GetFile(string fileName);
    • Stream downloads a specified file from the server to the client
  • int AddFile(string fileName);
    • Uses the chunk process and style of uploading a file from a client to a server.
  • void AddFileChunk(string fileName, int chunk, byte[] data);
    • This method takes care of adding file chunks on the server for the client to upload/add their file
  • void CompleteAddFile(string fileName, int chunks);
    • Call from the client to the server to indicate that all chunks are completed

FileInformation (Class for File Object)

The FileInformation class represents a file object in the application with both name (string) and size (bytes). In addition, there is a custom override of its ToString() object representation for display in the client form application list widow of both remote and local files. The object string representation formats the file size to either bytes, KB, MB, GB, etc. The code is available on GitHub [here].

FileManager Service Library

A class library describing the service implementation of the interfaces was added to my Visual Studio solution. Separating the service implementation from the host application project, provided one more level of abstraction and separation to better organize the solution. The code is available on GitHub [here].

FileManagerService (Code that Implements the Service Interface)

The service implementation code has service behaviors configured to allow for concurrency and multi-threading while each service call spins up a new service instance (PerCall). Note: In this project I did not utilize the multi-threading capability for the file chunking (file upload) demo. Β The service references a shared class library called β€œSharedLibrary” that contains the interface definition for the service and the class definition for a file object (FileInformation).Β  The code is available on GitHub [here].

Fields and Properties

The fields section contains a few notable private fields. The first sets an integer representing the chunk size (in bytes) while the second represents the private path on the server to where the file manager is allowing files to be listed, downloaded, and uploaded. The folder defaults to the personal MyDocuments folder, then opens (creates a new folder if not existing) a folder called FileManagerService. This folder then acts as the remote file management directory. To simplify the demo, additional features (ex: change of directory, custom) were not implemented. The FilePath property encapsulated and provided read access to the service file management directory.

Service Implementation Methods

The service implementations are described below.

List Files

Returns a list of files in the server directory.

Download File

Stream downloads a specified file from the server to the client

Upload File (Upload Process Part #1)

Uses the chunk process and style of uploading a file from a client to a server.

Add File Chunk (Upload Process Part #2)

This method takes care of adding file chunks on the server for the client to upload/add their file.

Complete File Chunking (Upload Process Part #3)

Call from the client to the server to indicate that all chunks are completed

Application Configuration App.Config

The application configuration simple sets up the service model configuration including the endpoint, interface contract name, basic http protocol, and the base address for this demo. This configuration must be included where the application is hosted (run) (see next section) and is displayed in this service class library project for reference. The code is available on GitHub [here].

FileManager Service Host (FileManagerServerHost)

The FileManagerServerHost project is a simple console application responsible for starting, managing, and stopping the File Manager service. This demo hosts the service using tcp protocol on a custom port address as described in the application configuration file.Β  The code is available on GitHub [here].

Main Program

The main program is the entry point for the service host application. It creates a ServiceHost object of type described in the FileManagerServiceLibrary, opens the host up for connections, and waits for the user to manually close the service (pressing enter in the console window).

Application Configuration App.Config

The application configuration simple sets up the service model configuration including the endpoint, interface contract name, basic http protocol, and the base address for this demo. The code is available on GitHub [here].

Client β€œTester to Service” Windows Form Application

The client β€œtester to service” is a simple windows form application project in the same solution that connects to the β€œFile Manager” by use of a ChannelFactory proxy to a specified service name that is defined in the client application configuration. The client program will use this proxy to test the OperationContract or methods available in the ServiceContract and return the results to the user on the form window.

The client application shares and references the β€œShared Library” – a class library project. It has access to the File Manager service interface to know exactly how it can consume the service with the help of a ChannelFactory proxy (discussed in the next section). The project code is available on GitHub [here].

Features

The client application was meant to only demonstrate consumption of the File Manager service and has limited scope of features. Some of the client application features are:

Asynchronous Processing: After a button is clicked, it spins a new asynchronous task complete with continuation options, cancellation, and its own Action delegate information.

Cancellation: The cancel button allows the user to asynchronously cancel any and all task threads running.

Local Directory Listing: The list box shows a formatted string describing the file name and sizes of all the files in the local directory.

Remote Directory Listing: The list box shows a formatted string describing the file name and sizes of all the files in the remote directory after the user clicks the β€œList Files” button.

Refresh: The Refresh button allows the immediate refresh of the local directory list box.

List Files: Starts an asynchronous task to call the service to retrieve a collection of the files available in the remote directory being hosted on the service.

Get File: Starts and asynchronous task to call the service to stream download the file selected by the user from the remote list box. Refreshes the local list box after the file is downloaded.

Add File: Starts and asynchronous task to call the service to upload the file (by chunking) selected by the user from the local list box. Refreshes the remote the list box after the file is uploaded.

Form Code

The code behind file is for the client tester and manages the application in one file. A separate business logic/process layer should had been added but the focus of the demo was on the service and consumption, not a UI/UX design. The code behind the form is available on GitHub [here].

Fields

  • Private fields allow the client to work with the following:
  • Cancellation
  • User selected file objects (from the ListBox control on the form)
  • Stream buffer size
  • Lists of local and remote file objects available
  • Path to the local client directory

Properties

The Local Directory is encapsulated in a property allowing it to be retrieved only. If it does not exist, it will attempt to create a β€œFileManagerLocal” folder in the user’s personal MyDocuments folder.

Constructors

When the form is instantiated, it does some house keeping on the form GUI (ex: button enable/disable)

Events

List Files Button Click

This updates the GUI, creates a new delegate, and passes action to the StartNewTask method (creates and starts a new task) with two parameters:

1.) the newly created action delegate pointing to the target method to call when the task starts and

2.) an enumeration defining the role as ServiceMenu.ListFiles (Listing Files)

Get File Button Click

This updates the GUI, creates a new delegate, and passes action to the StartNewTask method (creates and starts a new task) with two parameters:

1.) the newly created action delegate pointing to the target method to call when the task starts and

2.) an enumeration defining the role as ServiceMenu.GetFile (Get a File)

Refresh Local Directory Click

Add File Button Click

This updates the GUI, creates a new delegate, and passes action to the StartNewTask method (creates and starts a new task) with two parameters:

1.) the newly created action delegate pointing to the target method to call when the task starts and

2.) an enumeration defining the role as ServiceMenu.AddFile (Add a File)

Cancel Button Click

This calls the cancel command on the cancellation token so any asynchronous tasks running should soon cancel.

Methods

StartNewTask

This creates, configures, and starts a new thread task for the requested service call. Includes continuation and cancellation option features which include UI cleanup/refresh.

CallService

This section of code probably should had been on a separate business process layer, but it is currently in the form code. This creates a ChannelFactory proxy to the File Manager Service based on the settings in the client application configuration file. Based on the given enumeration type (ServiceMenu) it switches to the desired service call code block.

AddFile

This calls the service method to add a file.

GetFile

This calls the service method to get a file.

ListFiles

This calls the service to get a list of files on the remote directory.

UpdateGUIButtons

This updates the UI buttons

UpdateLocalFile

This updates the ListBox control on the UI showing the list of files in the user’s local directory.

UpdateRemoteFiles

This updates the ListBox control on the UI showing the list of files in the remote directory.

ChannelFactory Proxy and Application Configuration

The application configuration file (App.config) specifies the service endpoint address, binding, and contract information for the client to create a proxy and connect. The service contract information is in the Shared Class Library that has the service interface. The code is available on GitHub [here].

Demo

Code

Permission to publish my coding project using class coding demo examples and lab projects was granted by the professor. However, most of the code in this project is original to the student.

The entire project code repository is available on GitHub [here].

Kathleen has 15 yrs. of experience analyzing business IT needs and engineering solutions in payments, electric utilities, pharmaceutical, financial, virtual reality, and internet industries with a variety of technologies. Kathleen's project experience has been diverse ranging from executing the business analysis β€œdesign phase” to hands-on development, implementation, and testing of the IT solutions. Kathleen and her husband reside in Indiana with no children, instead living along with their fur babies.