Share via


WCF Hello World, Part 1: Building a Client

During the past month, we've taken a tour of the most important parts of the channel model.  We haven't really seen channels in action yet though.  This series of articles is a quick introduction to directly using the channel model to send and receive messages.  If you want to try these programs out, you'll need the February CTP of WCF.

Very few people are actually going to write a client application using the channel model.  This is what happens behind the scenes when you call new ChannelFactory<IMyWebService>() .

When I run these examples, I use two machines so that I can capture the packets going over the network.  You can run the programs on a single machine by just changing the client address to localhost.  In any case, you'll probably need to change the address to something besides the value I'm using unless you happen to have the same local network setup.

 using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

class Client
{
   static void Main(string[] args)
   {
      TextMessageEncodingBindingElement encoder = new TextMessageEncodingBindingElement();
      TcpTransportBindingElement transport = new TcpTransportBindingElement();
      transport.TransferMode = TransferMode.Streamed;
      CustomBinding binding = new CustomBinding(encoder, transport);
      IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>();
      factory.Open();
      EndpointAddress address = new EndpointAddress("net.tcp://192.168.0.2:5555/");
      IRequestChannel channel = factory.CreateChannel(address);
      channel.Open();
      Message request = Message.CreateMessage(factory.MessageVersion, "hello");
      Message reply = channel.Request(request);
      Console.Out.WriteLine(reply.Headers.Action);
      reply.Close();
      channel.Close();
      factory.Close();
   }
}

This is pretty much the minimal client application possible.  We're going to send a message with an action but no body to some other machine using TCP/IP.  I picked a text encoding so that we can look at the messages as they are being sent.  I also turned streaming on so that the request-reply channel shape is being used.

The procedure is that we

  1. build a binding that describes our channel stack,
  2. create a channel factory from the binding,
  3. open a channel pointing at the server,
  4. send a message to the server,
  5. and read the reply the server sends us back.

All of these concepts have appeared in past articles.

Although this client obviously isn't going to do much without a server, let's take a look at what gets sent over the wire if we run it.

 IP 192.168.0.3.3755 > 192.168.0.2.5555: tcp 0
    0x0000:  4500 0030 1c40 4000 8006 5d32 c0a8 0003  E..0.@@...]2....
  0x0010:  c0a8 0002 0eab 15b3 c360 0a62 0000 0000  .........`.b....
  0x0020:  7002 ffff 0fa9 0000 0204 05b4 0101 0402  p...............
IP 192.168.0.3.3755 > 192.168.0.2.5555: tcp 0
  0x0000:  4500 0030 1c64 4000 8006 5d0e c0a8 0003  E..0.d@...].....
  0x0010:  c0a8 0002 0eab 15b3 c360 0a62 0000 0000  .........`.b....
  0x0020:  7002 ffff 0fa9 0000 0204 05b4 0101 0402  p...............
IP 192.168.0.3.3755 > 192.168.0.2.5555: tcp 0
  0x0000:  4500 0030 1cb7 4000 8006 5cbb c0a8 0003  E..0..@...\.....
  0x0010:  c0a8 0002 0eab 15b3 c360 0a62 0000 0000  .........`.b....
  0x0020:  7002 ffff 0fa9 0000 0204 05b4 0101 0402  p...............

This is a trace of the TCP traffic arriving at the destination computer.  Each of these segments represents a connection attempt to the server.  We can knock, but nobody is home to answer.  Let's write a listener program now so that the client can do more than just time out.

Next time: WCF Hello World, Part 2: Building a Server

Comments

  • Anonymous
    March 28, 2006
    Ouch, that looks overcomplicated and messy. Is all this complexity really motivated?
  • Anonymous
    March 28, 2006
    If you want simplicity, you'd use the service framework, which can do the same client in two lines of code.  Most people are just going to write against a contract hosted in IIS without knowing how things like activation, routing, and dispatch work.

    This is the channel model, which is what you'd use if you wanted to write your own service framework.  There's no WSDL or contracts or Web Service anything down here.  Even things like SOAP and XML are optional, although I'm using SOAP in this example.  On the infrastructure side, this is a tremendous leap forward from writing in C++ against Winsock.
  • Anonymous
    March 28, 2006
    We often take for granted that we can send a message off to any computer in the world and that it will...
  • Anonymous
    March 28, 2006
    I added an update so that people won't think that directly using the channel model is what you'd normally do.
  • Anonymous
    March 29, 2006
    Writing a server that directly uses the channel model is not much more difficult than writing a client...
  • Anonymous
    March 31, 2006
    Over the last two days we've looked at building a simple client and server that directly use the channel...
  • Anonymous
    April 20, 2006
    I once again used the trick of taking the code from the previous example for the client. The File Transport...
  • Anonymous
    October 17, 2006
    Over the last two days we've looked at building a simple client and server that directly use the channel