Introduction to Windows Communication Foundation - Creating a Simple Web Service using WCF
8/13/2012The Project
This demo is to get a basic demo up and running so you can see some of the simple components needs to run a Window Communication Foundation web service. For this demo I will be using Visual Studio 2012 RC, but to my knowledge there is not anything different in the project defaults compared to 2010.
Start out by creating an empty solution project in Visual Studio:
Right click the solution and go to Add > New Project...
Select "WCF Service Library", call it ServiceLibrary and click ok:
When the project loads you will have two default class files. For the sake of the demo we will be starting from scratch, so delete both of those.
Now right click the ServiceLibrary project and add a new class. Call is MessageService.cs It should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServiceLibrary
{
class MessageService
{
}
}
Now that we have the basic project set up, we can start programming the service.
The Code
This service will be broken up into three different steps:
1. Set up the data contract
2. Set up the service contract
3. Implementing the service contract.
After that is done we will be able to use the built in WCF Testing harness to see how this all works.
The Data Contract
First lets take a look at what a data contract is.
A Data Contract is nothing more that a way that we can define what the body of the message we are sending will contain. It can be as simple as a single character, or as complex and a million line xml page. Either way, for WCF to understand what you want in the message, you need to define the Data Contract and the Data Members. Data members are the particular pieces of data inside the contract that you want public. If you are sending over user information, you will probably annotate things like FirstName and LastName with [DataMember], but things you don't want public you leave the annotation off (like for password or name or first born child)
For this example we are going to create a "Message" Data Contract that will have the senders name, the time it was sent, and the body of the message.
[DataContract] public class Message { [DataMember] public string Sender; [DataMember] public DateTime Timesent; [DataMember] public string Body; }
So now we have our basic object that we will work with and over this web service. On to step 2!
The Service Contract
Again, lets look at what a service contract is first:
In my opinion, a service contract is simple the actions that you can take of a data contract. So if a data contract tells you WHAT the data is, then the service contract tells you HOW you can manipulate or talk to that data. Basically, it is a list of actions that you are allowed to perform.
For the demo, I want to be able to add (or "send") a message, and also to be able to see a list of all the messages.
[ServiceContract] public interface IMessageService { [OperationContract] void SubmitMessage(Message message); [OperationContract] ListGetMessage(); }
So we have a SubmitMessage method that takes in our Message object, and a GetMessage method that returns a list of Message objects.
Quick note on the last two code snippets. Make sure that you add the following namespaces in order to resolve DataContract and ServiceContract respectively: System.Runtime.Serialization, System.ServiceModel
Implementing the service
Last step! We defined the data, and we define how we are going to talk to the data, so now lets implement our interface:
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] public class MessageService : IMessageService { Listmessages = new List (); public void SubmitMessage(Message message) { messages.Add(message); } public List GetMessage() { return messages; } }
Basic implementation. I create a new list of Message objects to hold them, and implement my two methods I defined above. Only thing of not here is the top line. I added this for the sake of the demo, meaning that it will only have one instance of the service running, so as I add messages to the service, they are persisted in memory, and we can call them when we invoke GetMessage. More than likely you will have some type of data storage in place, so this would not be needed then.
So that is what implementation looks like, now for a quick change in the config file. Remember how we deleted the template classes when the project was first created. Well now we need to update the config, because it is still looking for those. Need to change two lines:
Update the
And...
Update the
Once that is done hit F5 to debug. You will see an application come up that looks like this:
Double Click SubmitMessage() on the left, and fill in the Body and Sender fields with some text and click Invoke:
Since we are not returning anything, just inputting, the response will be empty. Click invoke a few more times, then double click GetMessages(). Once GetMessages comes up, you can see that we are not sending it any data, just retrieving. Click invoke and you should see something like this:
And there you have it. A very simple WCF web service. The next few posts will look at how to get a client application to talk to your service, and how we can format the message to return JSON rather than xml.
Regards
{David Stanley}