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

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

Rate This
  • Comments 7

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");

        host.Open();

 

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

        Console.WriteLine(client.CreateChannel().Echo("hello"));

    }

}

 

[ServiceContract]

interface IEcho

{

    [OperationContract]

    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?

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • 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

  • 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!

  • 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.

    http://tinyurl.com/ygfy469

  • 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)]

  • 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.

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

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

Page 1 of 1 (7 items)