using System; using System.Collections.Generic; using System.Text; namespace Artech.CAOFactory.Contract { publicinterface ICounter { int GetCount(); } }
ICounterFactory
using System; using System.Collections.Generic; using System.Text; namespace Artech.CAOFactory.Contract { publicinterface ICounterFactory { ICounter CreateService(); } }
Step 3 实现Contract:Artech.CAOFactory.Service
using System; using System.Collections.Generic; using System.Text; using Artech.CAOFactory.Contract; namespace Artech.CAOFactory.Service { publicclass CounterService : MarshalByRefObject,ICounter { privateint _count; ICounter Members#region ICounter Members publicint GetCount() { this._count++; returnthis._count; } #endregion } }
CounterFactoryService
using System; using System.Collections.Generic; using System.Text; using Artech.CAOFactory.Contract; namespace Artech.CAOFactory.Service { publicclass CounterFactoryService :MarshalByRefObject, ICounterFactory { ICounterFactory Members#region ICounterFactory Members public ICounter CreateService() { returnnew CounterService(); } #endregion } }
using System; using System.Collections.Generic; using System.Text; using Artech.CAOFactory.Contract; using System.Threading; using System.Runtime.Remoting; namespace Artech.CAOFactory.Client { class Program { conststring REMOTE_ADDRESS ="http://localhost/Artech.CAOFactory/CounterFactory.rem"; staticvoid Main(string[] args) { ICounterFactory counterFactory = (ICounterFactory)Activator.GetObject(typeof(ICounterFactory), REMOTE_ADDRESS); ICounter counter = counterFactory.CreateService(); while (true) { Console.WriteLine("The current value of the counter is {0}", counter.GetCount()); Thread.Sleep(5000); } } } }