WCF quick start

I was very scared before learning this new WCF animal, too many terms, too many configurations……

So how about start from a very simple one, like a Hello World application?

Step 1: Instead of creating a WCF service, just make it a separate class named MyGreetingService.

namespace MyService{
  public class MyGreetingService{
    public string GetGreetingMessagesFor(string userName){
      return "Hello " + userName + "! Have a nice day!";

The client will be

private void btnOK_Click(object sender, EventArgs e)
  MyGreetingService ms = new MyGreetingService();
  lblMessages.Text = ms.GetGreetingMessagesFor(txtUserName.Text);

OK, that was step 1. Next I want to make it a WCF service, by using Visual Studio wizard, (new item->WCF Service) or from scratch.

Step 2: Create a IWCFService.cs

using System.ServiceModel;
namespace MyService
  // NOTE: If you change the interface name "IWCFService1" here, you must also update the reference to "IWCFService1" in App.config in client side.
  public interface IWCFService1
    string GetGreetingMessagesFor(string userName);

Obviously, the MyGreetingService needs to change to implement this interface:

public class MyGreetingService : IWCFService1

Step 3: Do the work in client side.

3.1 Create a client side proxy class. Most people like to call this handler as proxy. So far I haven’t find any tool can auto-generate this proxy object based on the services/server. Or a nice Gui interface? This proxy class can be auto-generated and updated by “add web reference” in VSS project rmb menu items if you know the endpoint url, or wsdl. You can also write your own proxy class if you already got the interface, it’s official name is called service contract. An advanced way is creating a ChannelFactory class.

class WCFService1Client : ClientBase<IWCFService1>,  IWCFService1
  public WCFService1Client()
  public WCFService1Client(string endpointName):base(endpointName)
  public string GetGreetingMessagesFor(string userName)
    return Channel.GetGreetingMessagesFor(userName);

3.2 App.Config is the key part. I started from an in-proc WCF demo, which means server and client is in same process.

    <service name ="MyService.MyGreetingService" >
      <endpoint address ="net.pipe://localhost/MyPipe" binding ="netNamedPipeBinding"
        contract ="MyService.IWCFService1" />
    <endpoint name ="MyLocalEndPoint" address ="net.pipe://localhost/MyPipe"
      binding ="netNamedPipeBinding" contract ="MyService.IWCFService1" />

3.3 Start the server when app starts.

static void Main()
  ServiceHost host = new ServiceHost(typeof(MyService.MyGreetingService));
  Application.Run(new Form1());

3.4 We got another way to call greeting service. WCF way!

private void btnWCFOK_Click(object sender, EventArgs e)
  WCFService1Client proxy = new WCFService1Client("MyLocalEndPoint");
  lblMessages.Text = proxy.GetGreetingMessagesFor(txtUserName.Text);

Step 4 and further, you should be OK to create a standalone server by moving service section from app.config file into your server’s app.config.

Thanks to Juval Lowy’s book Programming WCF Services.


2 thoughts on “WCF quick start

  1. Pingback: Moving WCF Service to IIS « maonet technotes

  2. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you! By the way,
    how can we communicate?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s