Welcome to MSDN Blogs Sign in | Join | Help

Building a Custom File Transport, Part 3: Client

I once again used the trick of taking the code from the previous example for the client. The File Transport client uses the same binding class as the server. This isn't actually a hard requirement in WCF. We never force you to share code between the client and server if you don't want to. It's sufficient for the client and server bindings to merely be compatible. However, I didn't have a good reason to demonstrate this in the example so I reused the existing binding.

The timeouts I'm using for the client and server are somewhat arbitrary. File operations of this size shouldn't take five seconds unless you've got a very slow device or you're trying to access files on a network share. The server has a one minute timeout waiting for incoming connections. That should be plenty of time to go start a client for testing but still short enough that you can sit and watch the server terminate gracefully when the timeout expires.

The description of the message protocol is in yesterday's post about the server.

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

namespace FileRequestChannelClient
{
   class FileRequestChannelClient
   {
      static void Main(string[] args)
      {
         Console.Write("Creating factory...");
         Binding binding = new FileTransportBinding();
         IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>();
         factory.Open(TimeSpan.FromSeconds(5));
         Console.WriteLine(" done.");
         Console.Write("Creating channel...");
         using (factory)
         {
            Uri uri = new Uri("my.file://localhost/x");
            IRequestChannel channel = factory.CreateChannel(new EndpointAddress(uri));
            Console.WriteLine(" done.");
            Console.Write("Opening channel...");
            channel.Open(TimeSpan.FromSeconds(5));
            Console.WriteLine(" done.");
            while (true)
            {
               Console.Write("Enter some text (Ctrl-Z to quit): ");
               String text = Console.ReadLine();
               if (text == null)
               {
                  break;
               }
               Console.Write("Sending request...");
               Message requestMessage = Message.CreateMessage(MessageVersion.Default, "http://reflect", text);
               Message replyMessage = channel.Request(requestMessage, TimeSpan.FromSeconds(5));
               Console.WriteLine(" done.");
               using (replyMessage)
               {
                  Console.WriteLine("Processing reply: {0}", replyMessage.Headers.Action);
                  Console.WriteLine("Reply: {0}", replyMessage.GetBody<string>());
               }
            }
            channel.Close(TimeSpan.FromSeconds(5));
         }
      }
   }
}

Next time: Counting Down to TechEd 2006

Published Wednesday, April 19, 2006 5:00 AM by Nicholas Allen

Comments

Wednesday, April 19, 2006 12:14 PM by Nicholas Allen's Indigo Blog

# Building a Custom File Transport, Part 2: Server

For the server, I took the example code I posted a few weeks ago and started making modifications. I've...
Thursday, April 27, 2006 12:50 PM by Nicholas Allen's Indigo Blog

# Building a Custom File Transport, Part 6: Channel Factory

We've passed the midway point for the custom file transport example.  Now that we've got the underlying...
Tuesday, May 09, 2006 11:53 AM by Nicholas Allen's Indigo Blog

# Building a Custom File Transport, Part 11: Putting it Together

Today's article is just a summary of what we've put together with the file transport and a demonstration...
New Comments to this post are disabled
 
Page view tracker