1¸ÅÊö ͨ³£Çé¿öÏÂÎÒÃÇÔËÐгÌÐòµÄ¹ý³ÌÖлá²úÉúһЩÖмäÊý¾Ý£¬ÕâЩÖмäÊý¾ÝÐèÒªÔÚ½«À´µÄij¸öʱ¼ä¶ÁÈ¡¡£Õâ¾ÍÒªÇóÎÒÃÇÒª°ÑËü´æÔÚÒ»¸öÌṩ¸ßËÙ´æÈ¡µÄµØ·½£¬×îºÃµÄÑ¡Ôñ¾ÍÊÇÄÚ´æÖС£»ùÓÚÕâ¸öÒÔ¼°¶à¸öÔÒòÐèÒªÎÒÃÇ°ÑÕⲿ·Ö´æ´¢µ½ÆäËû»úÆ÷ÉÏ£¬ÕâÑù¾Í²úÉúÁË·Ö²¼Ê½»º´æµÄÎÊÌâ¡£ ʵ¼ÊÉÏ·Ö²¼Ê½»º´æ¸ù±¾ÉϾÍÊÇÌṩһ¸ö¸½¼ÓÄÚ´æÈÃÁíһ̨»úÆ÷°ïæ´æ´¢ºÍ²éÕÒÊý¾Ý¡£ 2ʵÏÖ·½·¨ Ê×ÏȽ¨Á¢Ò»¸ö¼¯ºÏ¶ÔÏ󣬸ü¯ºÏ¶ÔÏóÓ¦±£Ö¤Ḭ̈߳²È«¡£´úÂëÈçÏÂËùʾ Code 1 public static class MemObject 2 { 3 static MemObject() 4 { 5 MemObjl = new Dictionary<string, object>(); 6 } 7 8 public static Dictionary<string, object> Get() 9 { 10 if (MemObjl == null) 11 MemObjl = new Dictionary<string, object>(); 12 return MemObjl; 13 } 14 15 public static void Add(string key, object obj) 16 { 17 Dictionary<string, object> obg = Get(); 18 if (!obg.ContainsKey(key)) 19 obg.Add(key, obj); 20 } 21 22 public static void Remove(string key) 23 { 24 Get().Remove(key); 25 } 26 27 public static int Count() 28 { 29 return Get().Count; 30 } 31 32 public static object Get(string key) 33 { 34 Dictionary<string, object> obg = Get(); 35 if (obg.ContainsKey(key)) 36 return obg[key]; 37 return null; 38 } 39 40 public static bool Exits(string key) 41 { 42 return Get().ContainsKey(key); 43 } 44 45 private static Dictionary<string, object> MemObjl; 46 } ½Ó×ÅÎÒÃÇ°ÑËü°ü×°ÆðÀ´¿ÉÒÔͨ¹ýÔ¶³Ìµ÷Ó㬴úÂëÈçÏ Code 1 public class DataCatcher : MarshalByRefObject, ICarrier.ICarrier 2 { 3 public void Set(string key, object value) 4 { 5 //if (Exits(key)) 6 // Remove(key); 7 //MemObjl.Add(key, value); 8 MemObject.Add(key, value); 9 } 10 11 public bool Exits(string key) 12 { 13 return MemObject.Exits(key); 14 } 15 16 public void Remove(string key) 17 { 18 MemObject.Remove(key); 19 } 20 21 public int Count() 22 { 23 return MemObject.Count(); 24 } 25 26 public object Get(string key) 27 { 28 return MemObject.Get(key); 29 } 30 } ΪÁ˱ÜÃâÎÒÃǵÄÒµÎñÂ߼й¶ÎÒÃÇÏò¿Í»§¶ËÌṩ½Ó¿ÚÒÔ±ãµ÷Óà Code 1 public interface ICarrier 2 { 3 4 void Remove(string key); 5 6 bool Exits(string key); 7 8 void Set(string key,object value); 9 10 object Get(string key); 11 12 int Count(); 13 } ºÃÁË¡£ÕâÑùÎÒÃÇ·þÎñ¶ËµÄ´úÂë¾ÍËã¸ã¶¨ÁË¡£ ÏÂÃæÎÒÃÇÀ´·¢²¼·þÎñ¹©¿Í»§¶Ëµ÷Óà Code 1 public partial class SharpCatchedService : ServiceBase 2 { 3 public SharpCatchedService() 4 { 5 InitializeComponent(); 6 } 7 8 protected override void OnStart(string[] args) 9 { 10 TcpChannel channel = new TcpChannel(ConfigHelper.Port); 11 ChannelServices.RegisterChannel(channel, false); 12 RemotingConfiguration.RegisterWellKnownServiceType(typeof(DataCatcher), 13 "SharpCatched", WellKnownObjectMode.Singleton); 14 } 15 16 protected override void OnStop() 17 { 18 } 19 } ÕâÑù¿Í»§¶Ë¾Í¿ÉÒÔͨ¹ýÕâ¸ö½Ó¿ÚÀ´ÊµÏÖÔ¶³ÌÊý¾ÝµÄ´æÈ¡ ÔÚ¿Í»§¶ËÊ×ÏÈÎÒÃÇ»ñÈ¡Ô¶³ÌµÄ¶ÔÏó Code public static ICarrier Carrier() { ICarrier carrier = (ICarrier)Activator.GetObject(typeof(ICarrier), "tcp://localhost:" + ConfigHelper.Port + "/SharpCatched"); return carrier; } ½Ó×ÅÎÒÃÇ°üװһϠCode 1 public class SharpCatchedAPI 2 { 3 ICarrier icarrier; 4 5 public void Init() 6 { 7 icarrier = DoConnect.Carrier(); 8 } 9 10 public void Set(string key, object value) 11 { 12 icarrier.Set(key, value); 13 } 14 15 public void Remove(string key) 16 { 17 icarrier.Remove(key); 18 } 19 20 public object Get(string key) 21 { 22 return icarrier.Get(key); 23 } 24 25 public bool Exits(string key) 26 { 27 return icarrier.Exits(key); 28 } 29 } 3ºóÐø ÒÔÉÏʵÏÖµÄÊÇ×î»ù±¾µÄ·Ö²¼Ê½»º´æ½â¾ö·½°¸¡£ÆäʵÎÒÃÇ¿ÉÒÔ°ÑÕâ¸ö¼¯ºÏת»¯ÎªÆäËû¼¯ºÏ¶ÔÏó£¬ÀýÈçHashTable¡£ÔÚ¶ÔÏóÆô¶¯µÄʱºò¿ªÆôÒ»¸öÊØ»¤Ị̈߳¬Õâ¸ö½ø³Ì×öµÃ¹¤×÷¾ÍÊǰѵ½ÆڵĻº´æ¶ÔÏó×·¼Óµ½Ò»¸ö¼¯ºÏ¶ÔÏóÖУ¬È»ºó±éÀú¸Ã¶ÔÏóʵÏÖ»º´æ¶ÔÏóµÄÏú»Ù¡£ÎÒÃÇ»¹¿ÉÒ԰ѶÔÏó½øÐÐÒ»´Î¹þÏ£ÈöÔÏñÔÚ¶ą̀»º´æ·þÎñÆ÷ÉÏ´æ´¢ (ÔðÈα༣ºadmin) |