Net.Tcp Port Sharing Sample, Part 3

I'm presenting a small sample I wrote to demonstrate the port sharing feature. The third part is the test client and example usage. I'm looking for feedback to help make the sample better. Right now, the sample configures the endpoints in code rather than configuration because it uses randomly-generated addresses for the server. I may just tack on the configuration details at the end because other port sharing topics already cover that material.


You can use the test client to check that messages are correctly routed to services sharing the port.

 class client
{
   static void Main(string[] args)
   {
      Console.Write("Enter the service number to test: ");
      ushort salt = ushort.Parse(Console.ReadLine());
      string address = String.Format("net.tcp://localhost:5555/calculator/{0}", salt);
      ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(new NetTcpBinding());
      ICalculator proxy = factory.CreateChannel(new EndpointAddress(address));

      // Call the Add service operation.
      double value1 = 100.00D;
      double value2 = 15.99D;
      double result = proxy.Add(value1, value2);
      Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

      // Call the Subtract service operation.
      value1 = 145.00D;
      value2 = 76.54D;
      result = proxy.Subtract(value1, value2);
      Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

      // Call the Multiply service operation.
      value1 = 9.00D;
      value2 = 81.25D;
      result = proxy.Multiply(value1, value2);
      Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

      // Call the Divide service operation.
      value1 = 22.00D;
      value2 = 7.00D;
      result = proxy.Divide(value1, value2);
      Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

      Console.WriteLine();
      Console.WriteLine("Press <ENTER> to terminate client.");
      Console.ReadLine();

      factory.Close();
   }
}

Each instance of the service will print out its unique number and address. For instance, you may see the following text when you run service.exe.

 Service #4381 listening on net.tcp://localhost:5555/calculator/4381.
Press <ENTER> to terminate service.

Enter the service number you see here when you run client.exe.

 Enter the service number to test: 4381
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

Next time: More Binding Polymorphism

Comments

  • Anonymous
    August 09, 2006
    I'm presenting a small sample I wrote to demonstrate the port sharing feature.  The second part is the...
  • Anonymous
    September 19, 2006
    I wrote the article on MEX endpoints about a month ago after someone asked the question on one of our...
  • Anonymous
    October 08, 2006
    I wrote the article on MEX endpoints about a month ago after someone asked the question on one of our