Windows Communication Foundation: Hello World Application in IIS

Hello again,

In this post I will talk about the WCF platform and how to make an easy Hello World project and mounted in IIS 7.0

The first you need to know is that WCF is “…a platform rich in features necessary for building distributed service-oriented applications. With WCF developers can create services that satisfy a vast number of scenarios for the intranet and Internet including classic client-server applications, services that match the simplicity of ASP.NET web services (ASMX), services that incorporate the latest web service standards (WS*), and services that support advanced security scenarios such as federated security.”

I recommend to read this material to feel conformable with the terms around WCF but I can explain what are the basics Windows Communication Foundation Essentials

The basics of WCF are this:

  • A service Endpoint has an address, a Binding, and a Contract (ABC).
  • An Address is a network address indicates where the service is located.
  • A Binding specifies how a client can communicate with the endpoint including transport protocol (HTTP, TCP, Peer network, IPC (Inter-Process Communication over named pipes), MSMQ), encoding, and security requirements.
  • A Contract identifies what operations are available to the clients.

Ok it’s Code Time, let’s create a WCF Hello World Application, creating a New ASP.NET Web Application project with C#

First we need to define the Service Contract, named IHelloWorldService (yes it is an interface!)

using System.ServiceModel;

namespace HelloWorldWCF
    public interface IHelloWorldService
        string SayHello(string inputString);

About the attributes: [ServiceContract] and [OperationContract], these are attributes that define an interface as a Service Contract and a method to be exposed as a operation of the service contract. In this example is a very simple WCF Service that have a SayHello operation.

Then we need to create a Service that will implement the Service Contract:

namespace HelloWorldWCF
    public class HelloWorldService : IHelloWorldService
        public string SayHello(string inputString)
            return "Hello " + inputString;

As you can see, the Service HelloWorldService is implementing the Service Contract IHelloWorldService with their Operations (SayHello)

We almost done!
The next step is to define a host, in our case we’ll mount the service in IIS (to see the different host options see this link)

To do this we need to create the .svc that is basically a web page that have only one line of code, something like this:

<%@ ServiceHost Language="C#" Debug="true" Service="HelloWorldWCF.HelloWorldService" %>

This simple line tells the svc file which Service will expose, in our case, we need to define the namespace and the Service class.

After that we need to add some lines to the web.config

<?xml version="1.0"?>
			<service name="HelloWorldWCF.HelloWorldService" behaviorConfiguration="ServiceBehavior">
				<endpoint address="" binding="basicHttpBinding" bindingConfiguration="" contract="HelloWorldWCF.IHelloWorldService"/>
				<endpoint contract="IMetadataExchange" binding="mexHttpBinding" bindingConfiguration="" address="mex"/>
				<behavior name="ServiceBehavior">
					<serviceMetadata httpGetEnabled="true"/>
					<serviceDebug includeExceptionDetailInFaults="False"/>

As you can see I added tag that will contain all the configuration of our service, then I define a Service with the tag (with a name and a behavior configuration, that it’s defined below inside the tag) and two end points, one for metadata exchange with the contract IMetadataExchange and a mexHttpBinding binding (perfect to share metadata of the service to build clases and share info about the Service you expose), and other one to expose the service, in our case using the basicHttpBinding binding and defining which contract we will use, the IHelloWorldService contract associated with the address in our web page (that’s why I leave it empty)

Finally you can test the service, just click on Start Debugging and will appear something like this:

To test this service we could use a tool that is integrated in Visual Studio 2008 and Visual Studio 2010, just try this:

  1. Open the tool WcfTestClient.exe (I found it in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE)
  2. Then Add a New Service on File > Add Service
  3. Paste the address of the service, in my case http://localhost:4286/HelloWorldService.svc
  4. Then the program will create the information with the service metadata
  5. You will see something like the image below (image 1)
  6. Click in the SayHello() method to test it and provide some parameters (image 2) and click on Invoke
  7. View the results (image 3)

The next post I will talk about an application using the Windows Phone 7 SDK!


2 thoughts on “Windows Communication Foundation: Hello World Application in IIS

  1. renem1 says:

    Thanks, will try this one tonight 🙂

  2. mario says:

    Excellent!! Helped me a lot, thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: