Advise on developing a C# Client Server application

I am looking to develop a C# Client Server application which will be used to process inputs from barcode scanners on a factory floor and also from interfaces. This will require the Server program to accept input interactively from a user or via a batch file from an interface. I would like the Server to be a windows service and for it to implement logging and plug & play functionality to enable new logic to be added without requiring a new implementation of software.

I am looking for:

(a). Some good examples of Client/Server skeleton applications which implement either logging and/or plug & play that I can take some inspiration from and to enable me to build a solid foundation.

(b). Any tips/advise in developing a client/server application in c#    

Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
A Windows service is designed to control stuff on the computer on which it is running. It runs in the background, waiting for triggers to perform some work. These triggers are generally something like a timer, a change on the system or a command called by a program running on the machine itself.

That is similar in some ways to what a web service does, except that a web service has been designed for communications, for interaction between different computers or pieces of hardware. It's designed to receive calls from a physically external sources, which is not basically the case for a Windows service. As far as coding goes, a Web service is a lot like a dll providing a collection of classes that have only static methods.

It is thus a tool designed explicitly for what you intend to do.
You are right, WCF is a type of web services. However, while a web service works on a standard, WCF is a proprietary mechanism from Microsoft. When possible, I will prefer WCF over web services. Since web services are a standard, they stagnate because everybody always argue about what the next version will be. WCF evolves as fast new needs are encountered. And since it's not a multiplatform standard, it's a lot easier to use if you run everything in a Microsoft environment.

Here is the result of using the template that Visual Studio provides for a Windows Service:

namespace WindowsService1
	public partial class Service1 : ServiceBase
		public Service1()

		protected override void OnStart(string[] args)

		protected override void OnCustomCommand(int command)

		protected override void OnStop()

Open in new window

By default, a service sets itself up when it starts (OnStart method), settings timers, FileSystemWatchers or any other needed way being told when it need to react. Then it does nothing but waits for the trigger call or a message that it needs to cleanup because the service is stopping (OnStop).

I added the OnCustomCommand override, because you need to be able to communicate with the service. From the application that needs to "call" the service, you simply use a ServiceController object and call its ExecuteCommand method, passing an integer that is an indicator of what you want to do in the CustomCommand, most often a Select Case that decides which methods to run in the service.

This is the only entry point you can have in a Windows Service. You pass it an integer, and it reacts to it. If you want to send it something else that an integer, or more information, you need to something else, such as saving the data in a file before calling ExecuteCommand.

Note that OnCustomCommand returns void. You cannot get back any information from that. The role of a Windows service is to perform stuff on Windows, not to provide information.

Open in new window

Very limited as a service for interchange of data.

Now, here is the structure for a very basic WCF application. I removed the code itself because it is not pertinent for the demonstration. Sorry for the VB, I had this one on hand. The concept is the same in C#, only the syntax is different:

Public Interface IWoodwork
	Function Connect() As Boolean													'Used to test the connection
	Function GetMaterialType() As MaterialTypeCollection	'Returns a list of types of material (wood, furniture, etc.) that can be used in a ComboBox
	Function GetWoodInventory() As WoodCollection					'Returns the wood inventory
End Interface

Public Class Woodwork
	Implements IWoodwork

	Public Function Connect() As Boolean Implements IWoodwork.Connect
		'Used to test the connection
		Return True
	End Function

	Public Function GetMaterialTypes() As MaterialTypeCollection Implements IWoodwork.GetMaterialType
		'Returns a list of types of material (wood, furniture, etc.) that can be used in a ComboBox
		Return New MaterialTypeCollection
	End Function

	Public Function GetWoodInventory() As WoodCollection Implements IWoodwork.GetWoodInventory

		'Returns the wood inventory

		Dim read As WoodCollection

			read = New WoodCollection()

			Return read

		Catch ex As Exception
			Return Nothing

		End Try

	End Function

End Class

Open in new window

This is specifically created to let a distant application interact with a SQL Server database. I am not limited to 1 point of entry, I create as many as I want by adding a new method to the ServiceContract and implementing that method in the class. And they can return data, contrary to a Windows Service.

This is your server. Your client simply use it as if it was a dll

ObservableCollection<JBWoodwork.Service.WoodItem> woodInventory;
woodInventory=await service.GetWoodInventoryAsync();

And there it is, a complete inventory of the wood in the workshop.

In a Windows service, by default, all you can retrieve is a big void. You could always have the service create a file and save the data in the file, but they, would need to have a FileSystemWatcher is the calling application because there is no built-in ways for it to know when the command it called in the service has finished executing.

I think you can see why a Windows service is not a good solution for what you want to do (although I might be wrong, one needs to have all the little details before making such declarations with such certainty.)

For your software plug and play, you are asking for something that can be very involving. The way you explain your needs shows the problem: the user to have the capability of adding their own commands along with the code that the command is to execute. This implies that your users are programmers.

At some point, you could license VBA from Microsoft and let user program their own macros if the object model of your application was properly designed. But that it not a possibility anymore.

There is no way you can add code to the application. But there are mechanisms that can enables you to use outside code without having to recompile it. And as VBA did, in order to use these, the main application needs to have a very solid object model so that the outside code has the necessary mechanisms to act as they want. At the same time, since you are opening so that outside code can do stuff that the application was not designed to do, security might become a big concern. The object model should be permissive enough to let users to what they want, but at the same time police everything so as not to let them do things that you do not want they to do.

Referencing the application from another one, as one does when he controls Excel from a Windows application. Callback functions and events. Think that an event is code that you write that is called by a dll written by a programmer possibly many years ago.

But all of these mechanisms, although you do not have to reprogram and recompile the main application itself, imply programming somewhere. And while they are quite easy to implement when everything runs on the same computer, its can be a lot trickier to do it over the network other type of connection such as Wi-Fi.
Jacques Bourgeois (James Burger)PresidentCommented:
Why a server as a Windows Service? A server is usually on a remote system. A Windows Service runs locally.

As far as the server side is concerned, you are probably looking more at a web service or Windows Communication Foundations application (WCF).

And plug & play is usually associated with hardware, not software. Unless you do a very bad choice of scanner, you can almost be sure that it will be plug & play, which means that it will be recognized by Windows if it connects to a computer with any type of standard cable.

This being said, the first place to look is with the provider of the scanners. The scanner will be communicating with your system in some way. So how you design your server is in part dependent on the scanner itself. You thus need to know the characteristics of the scanner communication system, and possibly on the software that the scanner manufacturer provides with its hardware.

Will the scanner be communicating with your server through Wi-Fi, a http connection or will it be connected to a portable Windows station that will be used to communicate with the server?

If through WiFi or http then you probably need a service (not Windows service which is not the same thing) as a link between the scanner and your database.

If it is through a Windows station, the thing won't need to run when the scanner is not used, and then there is not need for a Windows Service. A standard Windows Application will be sufficient, easier to write, and will enable you to create your interfaces, something that a Windows Service cannot do. The application might then be able to write directly to the database through your network, or might need a service if you do not connect through the network.

So before you start thinking about the design or your software, you need to think about the organization of the hardware and the communication, and this is controlled in part by the type of scanner you will be using.

Client/server is nice. But since there can be many different types of clients, many different types of server and many different ways to communicate between them, there is no such thing as a universal "Client/Server skeleton applications".
pclarke7Author Commented:
Hi James,
Thanks for your comments. You raise a number of valid points so let me select some of these that require clarification.

Hardware configuration:
The Server application will be run on a remote application server with it's database situated on a separate SQL server.

The Client application will run on
(a). barcode scanners communicating through a Wi-Fi connection.
(b). on std windows platform with users inputting manually or via wedge scanners
(c). To cater for interfaces where there is no requirement for manual or scanned inputs the Server application will receive the interface input via a batch input file. So the Server job will be listening for inputs from 2 different sources, User input & interface input.

Windows Service Application V WCF
I don't understand why you say the server application could not run as a windows service application on the remote application server ? Can you explain why ?

I thought that WCF was more for  web services.
Question 1:
What are the advantages of running a WCF service application over a Windows service application

Plug & Play
When I talk about plug & play I am referring to the ability of adding additional functionality to the server application without having to modify/recompile the application. The server application will be driven be a series of rules & commands which can be configured into transactions. To give you a simple example , one of the commands that a transaction might utilize could be, RTV-NXTNBR, which would retrieve the next available unique number from a designated next number file. I want the user to have the capability of adding their own commands (eg. RTV-NXTNBR2) along with the code that the command is to execute and have the application pick up and execute this new command without have to re-compile the application.

I would still like to see some sample code for Window service applications. I know that there is no such thing as a universal "Client/Server skeleton application". I am just lloking for "Some" examples and I can then pick and chose elements that I want to incorporate.  
pclarke7Author Commented:
Thanks James,
for your excellent information on Client/Server. I will certainly now look at WCF in a lot more detail

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.