WCF 101: The Simplest WCF Example You’ll Ever See

Every once in a while, I’ll encounter a developer that thinks that WCF is too complicated to use. Whereas in fact, the basics of WCF are incredibly simple. MSDN’s tutorial hides this fact by making their introduction to WCF a fairly long and painful 6-step process, but you need much less to write your first WCF application. In fact, you don’t need Visual Studio and you don’t even need svcutil. All you need is notepad, and an install of .NET 3.0.


I have three steps for you:


1.       Copy the following code into a file called “WCF.cs” using your text editor of choice:


using System;

using System.ServiceModel;


class Program


    static void Main()


        ServiceHost host = new ServiceHost(typeof(Echo));

        host.AddServiceEndpoint(typeof(IEcho), new BasicHttpBinding(), “http://localhost/Service”);



        ChannelFactory<IEcho> client = new ChannelFactory<IEcho>(new BasicHttpBinding(), “http://localhost/Service”);






interface IEcho



    string Echo(string s);



class Echo : IEcho


    string IEcho.Echo(string s) { return s + ” echoooOOooo”; }



2.       Compile the code by running the C# compiler csc.exe against your new WCF.cs file. You can usually just run this in a command prompt:


C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe WCF.cs


3.       Still in the command prompt, run WCF.exe. You should get the output:


hello echoooOOooo


                indicating that you started a service, created a client, and made a call to your service’s “Echo” method.


Voila! That’s all you need to use WCF. Not too bad, right?

Comments (7)

  1. Raj says:

    Great WCF example!

    Couple of points..

    a) Command prompt must be run in ‘admin’ mode (Vista and 7)

    b) If you already have IIS running, try localhost:8080

  2. andyclap says:

    Lovely – I wish MS would start with "minimal" examples.

    Overengineered "best-practices" examples only serve to encourage cargo-cult programming; and MS’s idea of best-practices are sometimes slightly strange!

  3. Phil Bolduc says:

    My only comment, is that you are not closing your client connection.  This is misleading to a new developer.  They will run into the 10 connection timeout issue soon and be very confused. See the following link for a cheat sheet of WCF communication states.


  4. Bill says:

    In response to Raj, if you are running IIS7 you can decorate the class (Echo) with the following attribute and as long as your url is unique you will be able to run your app on port 80:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

  5. Steve says:

    I had a friend once who wrote a hello world application in C. When he was done he said "man, C is easy. I don't see how anyone has trouble with this."

    And there's the rub. The basics as you've shown them, are indeed quite simple but provide no value. Once you have to actually get any work done, WCF quickly becomes a huge, brittle, waste of time.

  6. murali krishna says:

    i executed the above code but cmd prompt is flashing and closing i cant able to see what is on the screen.

  7. Dave Timmermann says:

    Except that I get an addressAccessDeniedException: HTTP could not resister …. blah blah