Welcome to MSDN Blogs Sign in | Join | Help

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

Published Wednesday, August 09, 2006 5:00 AM by Nicholas Allen
Filed under: ,

Comments

Wednesday, August 09, 2006 12:11 PM by Nicholas Allen's Indigo Blog

# Net.Tcp Port Sharing Sample, Part 2

I'm presenting a small sample I wrote to demonstrate the port sharing feature.  The second part is the...
Wednesday, September 20, 2006 5:21 AM by Nicholas Allen's Indigo Blog

# An Unanticipated Addendum for Certain MEX Scenarios

I wrote the article on MEX endpoints about a month ago after someone asked the question on one of our...
Sunday, October 08, 2006 7:14 PM by Nicholas Allen's Indigo Blog

# An Unanticipated Addendum for Certain MEX Scenarios

I wrote the article on MEX endpoints about a month ago after someone asked the question on one of our
New Comments to this post are disabled
 
Page view tracker