Wednesday, November 25, 2009

WCF Services

Next up: Exam 70-503: Windows Communication Foundation

Windows Communication Foundation Notes:

WCF was released as part of .NET 3.0, initially code-named Indigo. It is intended to support distributed applications and evolve from past attempts such as Remoting, Web Services, DCOM, and MSMQ.

It is focused on hosting, consuming, securing services to support distributed application.

It was enhanced as part of .NET 3.5 to support integration with Windows Workflow Foundation, as well as Web-based application support.

The "ABCs" of WCF are:

  • Address

  • Binding

  • Contract

Communication between WCF Clients and Service are done via channels. Messages are passed through the channel. The channel itself is composed of binding elements, which are stacks. The service endpoint provides a contract / interface. The WCF binding will define the channel, and there are built in bindings, such as BasicHttpBinding, WSHttpBinding, NetTcpBinding, and others.

Here is the list and definition of WCF System-Provided Bindings.

I wrote the following as my first WCF Service, based on examples in the book and on the Web.

using System;
using System.ServiceModel;
using System.Text;

namespace WCF
public interface IGreetingService
String GetGreeting(String firstName, String lastName);

public class GreetingService : IGreetingService
public string GetGreeting(String firstName, String lastName)
StringBuilder greeting = new StringBuilder();
greeting.Append("Hello ");
greeting.Append(" ");
greeting.Append(" ");
return greeting.ToString();

public class Service
public static void Main(string[] args)
ServiceHost serviceHost = new ServiceHost(typeof(GreetingService), new Uri("http://localhost:8000/wcfTest"));
serviceHost.AddServiceEndpoint(typeof (IGreetingService), new BasicHttpBinding(), "");

Console.WriteLine("Running GreetingService, hit any key to end.");

The code sample above uses programmatic definition of the Service Address URI as well as the Bindings.

However, we could also declaratively define these in a web.config or app.config Configuration file.

Here is an example of an app.config file that provides the address, bindings, and exposes the Metadata Exchange information.

<?xml version="1.0" encoding="utf-8" ?>
<service name="WCF.GreetingService" behaviorConfiguration="greetingServiceBehavior">
<add baseAddress="http://localhost:8000/wcfTest"/>
<endpoint address=""
contract="WCF.IGreetingService" />
<endpoint address="mex"

<behavior name="greetingServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>

When I open the URL: http://localhost:8000/wcfTest, I see the following in my browser:

No comments:

Post a Comment