using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; namespace Artech.MyRemoting.Hosting { class Program { staticvoid Main(string[] args) { RemotingConfiguration.Configure("Artech.MyRemoting.Hosting.exe.config", false); Console.WriteLine("The Calculator services have begun to listen"); Console.Read(); } } }
很简单,无需赘言。
4. 编写客户端
using System; using System.Collections.Generic; using System.Text; using Artech.MyRemoting.RemoteService; using System.Runtime.Remoting; using System.Threading; namespace Artech.MyRemoting.Client { class Program { staticvoid Main(string[] args) { string singelCallAddress ="http://localhost:1234/artech.myremoting/singlecall.calculator.rem"; string singletonAddress ="http://localhost:1234/artech.myremoting/singleton.calculator.rem"; string caoAddress ="http://localhost:1234/artech.myremoting"; RemotingConfiguration.RegisterActivatedClientType(typeof(CalculatorService), caoAddress); Console.WriteLine("Create server SingleCall proxy at {0}", DateTime.Now.ToString("hh:mm:ss")); CalculatorService singleCallCalculator = Activator.GetObject(typeof(CalculatorService), singelCallAddress) as CalculatorService; Thread.Sleep(10000); Console.WriteLine("Create server Singleton proxy at {0}", DateTime.Now.ToString("hh:mm:ss")); CalculatorService singletonCalculator = Activator.GetObject(typeof(CalculatorService), singletonAddress) as CalculatorService; Thread.Sleep(10000); Console.WriteLine("/nCreate client activated object proxy at {0}", DateTime.Now.ToString("hh:mm:ss")); CalculatorService caoCalculator =new CalculatorService(); Thread.Sleep(10000); Console.WriteLine("/nCall the method of SingleCall object at {0}", DateTime.Now.ToString("hh:mm:ss")); InvocateCalculator(singleCallCalculator); Thread.Sleep(10000); Console.WriteLine("/nCall the method of Singleton object at {0}", DateTime.Now.ToString("hh:mm:ss")); InvocateCalculator(singletonCalculator); Thread.Sleep(10000); Console.WriteLine("/nCall the method of CAO object at {0}", DateTime.Now.ToString("hh:mm:ss")); InvocateCalculator(caoCalculator); Console.WriteLine("The times to invovate the current SingleCall remote object is {0}", singleCallCalculator.GetCallCount()); Console.WriteLine("The times to invovate the current Singleton remote object is {0}", singletonCalculator.GetCallCount()); Console.WriteLine("The times to invovate the current CAO remote object is {0}", caoCalculator.GetCallCount()); Console.Read(); } staticvoid InvocateCalculator(CalculatorService calculator) { Console.WriteLine("x + y = {2} where x = {0} and y = {1}",1,2,calculator.Add(1,2)); } } }
string singelCallAddress ="http://localhost:1234/artech.myremoting/singlecall.calculator.rem"; string singletonAddress ="http://localhost:1234/artech.myremoting/singleton.calculator.rem"; string caoAddress ="http://localhost:1234/artech.myremoting"; RemotingConfiguration.RegisterActivatedClientType(typeof(CalculatorService), caoAddress); Console.WriteLine("Create server SingleCall proxy at {0}", DateTime.Now.ToString("hh:mm:ss")); CalculatorService singleCallCalculator = Activator.GetObject(typeof(CalculatorService), singelCallAddress) as CalculatorService; Thread.Sleep(10000); Console.WriteLine("Create server Singleton proxy at {0}", DateTime.Now.ToString("hh:mm:ss")); CalculatorService singletonCalculator = Activator.GetObject(typeof(CalculatorService), singletonAddress) as CalculatorService; Thread.Sleep(10000); Console.WriteLine("/nCreate client activated object proxy at {0}", DateTime.Now.ToString("hh:mm:ss")); CalculatorService caoCalculator =new CalculatorService();
依次调用三个Proxy的Add方法显示调用的准确时间
Thread.Sleep(10000); Console.WriteLine("/nCall the method of SingleCall object at {0}", DateTime.Now.ToString("hh:mm:ss")); InvocateCalculator(singleCallCalculator); Thread.Sleep(10000); Console.WriteLine("/nCall the method of Singleton object at {0}", DateTime.Now.ToString("hh:mm:ss")); InvocateCalculator(singletonCalculator); Thread.Sleep(10000); Console.WriteLine("/nCall the method of CAO object at {0}", DateTime.Now.ToString("hh:mm:ss")); InvocateCalculator(caoCalculator);
获得他们的计数器,看看调用次数有何不同。
Console.WriteLine("The times to invovate the current SingleCall remote object is {0}", singleCallCalculator.GetCallCount()); Console.WriteLine("The times to invovate the current Singleton remote object is {0}", singletonCalculator.GetCallCount()); Console.WriteLine("The times to invovate the current CAO remote object is {0}", caoCalculator.GetCallCount());