Windows Communication Foundation
Windows Communication Foundation
OVERVIEW
THE BASICS
There are a few basic tasks when creating a WCF service. The basic tasks that must be
performed are, in order:
1. Define the service contract. A service contract specifies the signature of a service,
the data it exchanges, and other contractually required data.
2. Implement the contract. To implement a service contract, create the class that
implements the contract and specify some custom behaviors that the runtime should
have.
3. Configure the service by specifying endpoint information and other behavior
information.
4. Host the service in an application.
5. Build a client application.
EXAMPLE
To display a step-by-step enactment of the above steps, let’s define a web service that
reports the current date/time:
First, let’s create a project to house our service contract. Let’s call it
WCFDateTime.Service.
1.2 DEFINE A SERVICE CONTRACT
Then, let’s define an interface that represents the service we’re going to provide. In
this case, let’s call it IDateTimeService.
public interface IDateTimeService
{
}
We need to add a method to our service that will return the current date/time:
public interface IDateTimeService
{
DateTime GetCurrentDateTime();
}
Now, we need to decorate our code with attributes so that WCF will recognize our
interface and its methods. Namely, we will add the following attributes:
• ServiceContractAttribute
o Identifies an interface as a WCF service contract
• OperationContractAttribute
o Identifies methods of an interface as WCF service operations, that is,
methods that can be called through WCF
using System.ServiceModel;
[ServiceContract]
public interface IDateTimeService
{
[OperationContract]
DateTime GetCurrentDateTime();
}
In order to use the above attributes, you’ll need to add a reference to the
System.ServiceModel assembly. The service contract is now ready to be used.
Now that we’ve defined our service contract, we can move on to implementing it.
First, let’s create another project to house our server application. Let’s call it
WCFDateTime.Server, and make it a Console Application project.
2.2 IMPLEMENT THE CONTRACT
The above code will obviously not compile, because we did not implement the
GetCurrentDateTime method:
public class DateTimeService : IDateTimeService
{
public DateTime GetCurrentDateTime()
{
return DateTime.Now;
}
}
That’s it! The DateTimeService is now ready to be used.
The address specifies the address where you want the service to be located.
The binding specifies the transport you want to use in order to provide the service.
The contract specifies what service will be provided
4.1 HOST THE SERVICE IN AN APPLICATION
Now that WCF has been configured, the simplest way to host this service is to create
a Console Application and use the ServiceHost class. First, add a reference to the
System.ServiceModel assembly. Then, In the program.cs file of
WCFDateTime.Server, do the following:
using System.ServiceModel;
namespace WCFDateTime.Server
{
class Program
{
static public void Main(string[] args)
{
// Get a host for our service
ServiceHost serviceHost = new ServiceHost(
typeof(DateTimeService)
);
By simply running this application, we expose our service so that client applications
can use it.
5.1 BUILD A CLIENT APPLICATION
To build a client application, we must create a new project for it. Let’s call it
WCFDateTime.Client, and make it a Console Application as well.
Now, we need to add an Application Configuration file, and give it some settings that
will allow us to connect to the service we just created:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<client>
<endpoint
address="http://localhost:8081/DateTimeService"
binding="wsHttpBinding"
contract="WCFDateTime.Service.IDateTimeService"
name="MyDateTimeService">
</endpoint>
</client>
</system.serviceModel>
</configuration>
Now that our we’ve configured our WCF client, we can connect to it. There are two
ways to connect to the WCF service we previously created:
class Program
{
public static void Main(string[] args)
{
// Get a channel factory for our service,
// using the configuration for "MyDateTimeService"
// in the application configuration file.
ChannelFactory<IDateTimeService> channelFactory =
new ChannelFactory<IDateTimeService>("MyDateTimeService");
Your client application is now ready to run! Simply start the WCFDateTime.Server
application, and once it’s running, run the WCFDateTime.Client application to see it
work!