Messaging technology allows a client and a server to send a message to each other, and they do not have to run at the same time. One party can send a message to another party at anytime. A crash on one side does not effect the another side.
In this tutorial, I am going to implement a simple WCF client-server application without any consideration on security. We are going to use MSMQ as a transport mechanism. I use Microsoft Visual Studio 2008. Please read Queuing in WCF msdn page for further information. Note: you have to install MSMQ on your computer.
Please refer to this msn page for what is WCF (Windows Communication Foundation) and what does it solves.
Please refer to this msdn page for various WCF technical term.
Each WCF service has an endpoint. An endpoint consists fo address, binding and contract.
– An address is usually a URI which specifies where the endpoint of the service is.
– A binding defines the transport mechanism between a client and a server. There are some predefined bindings avaiable. In this tutorial, we are going to use MSMQ as a transport mechanism, so we are interested in NetMSMQBinding.
– A contract is your service, and it is implemented in C# in this tutorial.
1. Create a server project
Create a server project by simply select a C# console application from “New Project” dialog. Let’s name the solution name as “wcf_tutorial” and the server project name as “WCFServer1.”
You need to add two references which are System.messaging (found in .NET tab when opening up the “Add Reference” dialog) and System.ServiceModel.dll (found in “C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Communication Foundation” in Windows folder, click on “Browse” tab in the “Add Reference” dialog).
2. Create server source files
IWork is an interface which will be configure as a contract in a binding configuration. WorkService is the implementation of this contract. The server does nothing. It just receives a string from a client.
Please refer to How to: Define a Windows Communication Foundation Service Contract msdn page for how define a contract. Please remember than a MSMQ is a messaging technology which is anynchronous communication. There is no respond back from a server like web service. Thus, the function has no return type which is “void.”
– Please refer to Build a Queued WCF Response Service for more information on WCF and MSMQ.
– Please refer to How to: Implement a Windows Communication Foundation Service Contract msdn page for how to implement a contract.
[ServiceContract(Namespace = “https://sukasom.wordpress.com”)]
[OperationContract(IsOneWay = true)]
void submitWork(String str1);
public class WorkService : IWork
public void submitWork(String str1)
Console.WriteLine(“Server has received a work : “ + str1);
static void Main(string args)
string queueName = ConfigurationSettings.AppSettings[“queueName”];
using (ServiceHost serviceHost = new ServiceHost(typeof(WorkService)))
Console.WriteLine(“The service is ready.”);
Console.WriteLine(“Press <ENTER> to terminate service.”);
Put the above code in the Program.cs. The server part is modified from How to: Host a WCF Service in a Managed Application msdn page.
3. Configure server configuration file
You need to configure the configuration file. Add an App.config file into your project. Please refer to Specifying an Endpoint Address msdn page for more information on address configuration. My configuration file is modified from NetMSMQBinding msdn page.
<?xml version=”1.0” encoding=”utf-8” ?>
<add key=”queueName” value=”.\private$\ServiceModelSamples” />
<service name=”WCFServer1.WorkService” behaviorConfiguration=”Behavior1“>
<!– Following is necessary for getting information about the service through http with the svcutil.exe tool. –>
<serviceMetadata httpGetEnabled=”true” httpGetUrl=”http://localhost:8000/Hello/” />
<!– For first example, the security is just disabled. –>
In the “service” element, an endpoint is defined with an address, a binding and a contract. This WCF sample application uses MSMQ as a transport mechanism, so the binding is the NetMsmqBinding.
The contract specifies the interface which is WCFServer1.IWork. The security is turned off in this tutorial through the bindingConfiguration named “test.”
A client needs to be able to access the metadata of the service, so it is defined through the behaviorConfiguration named “Behavior1″ where the httpGetEnabled attribute is set to true. It means that the metadata can be retrieved through the http through the URL set in the httpGetUrl attribute which is “http://localhost:8000/Hello/” in this tutorial. This URL allows the client to get a metadata about the service from the correct location. You can open up the “http://localhost:8000/Hello/“ on the browser as shown below.
More info on address and httpGetEnabled can be found in WCF Part 6 : Address blog.
4. Implement the client project.
Start a visual studio 2008 and add a new C# console project as shown in the figure below. The name of the project is “WCFClient1.” Just like the server part, add the reference to System.ServiceModel.dll. More info on accessing service through a client at Accessing Services Using a WCF Client msdn page.
5. Generate a client proxy.
The ServiceModel Metadata Utility Tool (Svcutil.exe) is needed to generate the proxy file for a client. The proxy file allows you to treat a remote service as a local service. You simply instantiate a remote object and call a function on it. This is much simple compared with some old distributed technologies.
Please refer to ServiceModel Metadata Utility Tool (Svcutil.exe) msdn page for the manaul of Svcutil.exe.
Please start the server first then type the following command in the command prompt.
“C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcUtil.exe” http://localhost:8000/Hello/
The URI is the URI that is defined in the App.config in the server part. After running the above command, you will get two files which are WorkService.cs and output.config.
Please rename the output.config to App.config. Copy those two files into your project directory, and add those files into your project.
Following is the code for the client:
////////////Program.cs in the client part/////////////
static void Main(string args)
WorkClient wc = new WorkClient();
wc.submitWork(“hello work 1″);
Calling a server is very simple, instantiating the proxy object and calling a function on it. You can see that there is a class called WorkClient defined in the WorkService.cs. Please run the server first. If the queue has not been created, it will be created. When the queue is created, you don’t have to run the server before running the client. You can run the client first, and the message from the client will be stored in the queue. The message will be sent to the server when the server is started again.
You can take a look Tom Hollander’s blog for another good tutorial on this topic.
The book from [Lowy] is also good.