// 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.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using Remoting.Common; namespace Remoting.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 = (ITest)Activator.GetObject(typeof(ITest), string.Format("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 = (ITest)Activator.GetObject(typeof(ITest), string.Format("tcp://{0}:{1}/{2}", SERVER, SERVER_PORT, name)); for(int i = 0; i < 2; i++) { int n = tst1.GetCounter(); Console.WriteLine(n); } ITest tst2 = (ITest)Activator.GetObject(typeof(ITest), string.Format("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 = 100000; private static void TestPerformance(string name) { ITest tst = (ITest)Activator.GetObject(typeof(ITest), string.Format("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)); } private static void Test(string label, string name) { Console.WriteLine(label + ":"); TestFunctional(name); TestInstantiation(name); TestPerformance(name); } public static void Main(string[] args) { ChannelServices.RegisterChannel(new TcpClientChannel(), false); Test("Singleton", NAME1); Test("SingleCall", NAME2); } } }