// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using WCF.Common; namespace WCF.Client { public class Program { private const string SERVER = "localhost"; private const int SERVER_PORT = 12345; private const string NAME1 = "Test1"; private const string NAME2 = "Test2"; private static void TestFunctional(string name) { ITest tst = ChannelFactory.CreateChannel(new NetTcpBinding(), new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name))); int a = 123; int b = 456; int c = tst.Add(a, b); Console.WriteLine(c);; String s = "ABC"; String s2 = tst.Dup(s); Console.WriteLine(s2); Data d = new Data(123, "ABC"); Data d2 = tst.Process(d); Console.WriteLine("{0} {1}", d2.Iv, d2.Sv); } private static void TestInstantiation(string name) { ITest tst1 = ChannelFactory.CreateChannel(new NetTcpBinding(), new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name))); for(int i = 0; i < 2; i++) { int n = tst1.GetCounter(); Console.WriteLine(n); } ITest tst2 = ChannelFactory.CreateChannel(new NetTcpBinding(), new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name))); for(int i = 0; i < 2; i++) { int n = tst2.GetCounter(); Console.WriteLine(n); } } private const int REP = 10000; private static void TestPerformance(string name) { ITest tst = ChannelFactory.CreateChannel(new NetTcpBinding(), new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name))); DateTime dt1 = DateTime.Now; Enumerable.Range(0, REP).AsParallel().ForAll(i => { tst.Noop(); }); DateTime dt2 = DateTime.Now; Console.WriteLine("{0} requests per second", (int)(REP / (dt2 - dt1).TotalSeconds)); } public static void Test(string label, string name) { Console.WriteLine(label + ":"); TestFunctional(name); TestInstantiation(name); TestPerformance(name); } public static void Main(string[] args) { Test("Single", NAME1); Test("PerCall", NAME2); } } }