Ô´ÂëÍø,Ô´ÂëÂÛ̳,Ô´ÂëÖ®¼Ò,ÉÌÒµÔ´Âë,ÓÎÏ·Ô´ÂëÏÂÔØ,discuz²å¼þ,ÆåÅÆÔ´ÂëÏÂÔØ,¾«Æ·Ô´ÂëÂÛ̳

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á
Â¥Ö÷: ttx9n

[ASP.NET] Microsoft .Net RemotingϵÁн̳ÌÖ®Èý:Remotingʼþ´¦ÀíÈ«½Ó´¥

[¸´ÖÆÁ´½Ó]

7Íò

Ö÷Ìâ

861

»ØÌû

32Íò

»ý·Ö

ÂÛ̳ԪÀÏ

Rank: 8Rank: 8

»ý·Ö
329525
·¢±íÓÚ 2016-5-30 10:21:43 | ÏÔʾȫ²¿Â¥²ã |ÔĶÁģʽ
±¾ÎÄÖ÷Òª½²½â.Net RemotingÖеÄRemotingʼþ´¦Àí£¬ÐèÒªµÄÅóÓÑ¿ÉÒԲο¼Ï¡£

¡¡¡¡Ç°ÑÔ£ºÔÚRemotingÖд¦ÀíʼþÆäʵ²¢²»¸´ÔÓ£¬µ«ÆäÖÐÓÐЩ¼¼ÇÉÐèÒªÄãÈ¥ÍÚ¾ò³öÀ´¡£ÕýÊÇÕâЩ¼¼ÇÉ£¬·Â·ðÉ­ÑϵıÚÀÝ£¬ÈÃÐí¶àÈËÍû¶øÉú磬»òÕßÊDz»ÖªËùν£¬×îºó·ÅÆúÁËʼþÔÚRemotingµÄʹÓ᣹ØÓÚÕâ¸öÖ÷Ì⣬ÔÚÍøÉÏÒ²ÓкܶàÌÖÂÛ£¬Ïà¹ØµÄ¼¼ÊõÎÄÕÂÒ²²»ÉÙ£¬Òź¶µÄÊÇ£¬ºÜ¶àÎÄÕ¸ÅÊöµÄ¶¼²»Ì«È«Ãæ¡£ÎÒÔÚÑо¿RemotingµÄʱºò£¬Ò²¶Ôʼþ´¦Àí·¢ÉúÁËÐËȤ¡£¾­¹ý²Î¿¼Ïà¹ØµÄÊé¼®¡¢Îĵµ£¬²¢¾­¹ý·´¸´µÄÊÔÑ飬ÉîÐÅ×Ô¼ºÄܹ»°ÑÕâ¸öÎÊÌâ²ûÊöÇå³þÁË¡£

¡¡¡¡±¾ÎĶÔÓÚRemotingºÍʼþµÄ»ù´¡ÖªÊ¶²»ÔÙ½éÉÜ£¬ÓÐÐËȤµÄ¿ÉÒÔ¿´ÎÒµÄϵÁÐÎÄÕ£¬»ò²éÔÄÏà¹ØµÄ¼¼ÊõÎĵµ¡£

±¾ÎÄʾÀý´úÂëÏÂÔØ£º

Remotingʼþ(¿Í»§¶Ë·¢´«Õæ)

Remotingʼþ(·þÎñ¶Ë¹ã²¥)

Remotingʼþ(·þÎñ¶Ë¹ã²¥¸Ä½ø)

¡¡¡¡Ó¦ÓÃRemoting¼¼ÊõµÄ·Ö²¼Ê½´¦Àí³ÌÐò£¬Í¨³£°üÀ¨Èý²¿·Ö£ºÔ¶³Ì¶ÔÏó¡¢·þÎñ¶Ë¡¢¿Í»§¶Ë¡£Òò´Ë´ÓʼþµÄ·½ÏòÉÏ¿´£¬¾ÍÓ¦¸ÃÓÐÈýÖÖÐÎʽ£º

1¡¢·þÎñ¶Ë¶©ÔÄ¿Í»§¶Ëʼþ
2¡¢¿Í»§¶Ë¶©ÔÄ·þÎñ¶Ëʼþ
3¡¢¿Í»§¶Ë¶©ÔÄ¿Í»§¶Ëʼþ

¡¡¡¡·þÎñ¶Ë¶©ÔÄ¿Í»§¶Ëʼþ£¬¼´ÓÉ¿Í»§¶Ë·¢ËÍÏûÏ¢£¬·þÎñ¶Ë²¶×½¸ÃÏûÏ¢£¬È»ºóÏìÓ¦¸Ãʼþ£¬Ï൱ÓÚϼ¶ÏòÉϼ¶·¢´«Õæ¡£·´¹ýÀ´£¬¿Í»§¶Ë¶©ÔÄ·þÎñ¶Ëʼþ£¬ÔòÊÇÓÉ·þÎñ¶Ë·¢ËÍÏûÏ¢£¬´Ëʱ£¬ËùÓпͻ§¶Ë¾ù²¶»ñ¸ÃÏûÏ¢£¬¼¤·¢Ê¼þ£¬Ï൱ÓÚÊÇÒ»¸öϵͳ¹ã²¥¡£¶ø¿Í»§¶Ë¶©ÔÄ¿Í»§¶ËʼþÄØ£¿¾ÍÀàËÆÓÚÁÄÌìÁË¡£ÓÉij¸ö¿Í»§¶Ë·¢³öÏûÏ¢£¬ÆäËû¿Í»§¶Ë²¶»ñ¸ÃÏûÏ¢£¬¼¤·¢Ê¼þ¡£¿ÉϧµÄÊÇ£¬ÎÒ²¢Ã»ÓÐÕÒµ½Ë½ÁĵĽâ¾ö°ì·¨¡£µ±¿Í»§¶Ë·¢³öÏûÏ¢ºó£¬Ö»Òª¶©ÔÄÁ˸ÃʼþµÄ£¬¶¼»á»ñµÃ¸ÃÐÅÏ¢¡£

¡¡¡¡È»¶ø²»¹ÜÊÇÄÄÒ»ÖÖ·½Ê½£¬¾¿ÆäʵÖÊ£¬ÕæÕý°üº¬Ê¼þµÄ»¹ÊÇÔ¶³Ì¶ÔÏó¡£Ô­ÀíºÜ¼òµ¥£¬ÎÒÃÇÏëÒ»Ï룬ÔÚRemotingÖУ¬¿Í»§¶ËºÍ·þÎñ¶Ë´«µÝµÄÄÚÈÝÊÇʲôÄØ£¿ÎãÓ¹ÖÃÒÉ£¬ÊÇÔ¶³Ì¶ÔÏó¡£Òò´Ë£¬ÎÒÃÇ´«µÝµÄʼþÏûÏ¢£¬×ÔÈ»ÊDZ»Ô¶³Ì¶ÔÏóËù°ü¹ü¡£Õâ¾ÍÏñEMS¿ìµÝ£¬Ô¶³Ì¶ÔÏóÊÇÔËËÍÐżþµÄÆû³µ£¬¶øʼþÏûÏ¢¾ÍÊÇÆû³µËù×°ÔصÄÐżþ¡£ÖÁÓÚʼþ´«µÝµÄ·½Ïò£¬Ö»ÊÇ·¢ËÍÕߺͶ©ÔÄÕߵĽÇÉ«·¢ÉúÁ˸ıä¶øÒÑ¡£

Ò»¡¢ ·þÎñ¶Ë¶©ÔÄ¿Í»§¶Ëʼþ

¡¡¡¡·þÎñ¶Ë¶©ÔÄ¿Í»§¶Ëʼþ£¬Ïà¶Ô±È½Ï¼òµ¥¡£ÎÒÃǾÍÒÔ·¢´«ÕæΪÀý¡£Ê×ÏÈ£¬ÎÒÃDZØÐë¾ß±¸´«Õæ»úºÍÒª´«ÕæµÄÎļþ£¬Õâ¾ÍºÃ±ÈÎÒÃǵÄÔ¶³Ì¶ÔÏ󡣶øÇÒÕâ¸ö´«Õæ»úÉϱØÐë¾ß±¸¡°·¢ËÍ¡±µÄ²Ù×÷°´Å¥¡£Õâ¾ÍºÃ±ÈÊÇÔ¶³Ì¶ÔÏóÖеÄÒ»¸öίÍС£µ±¿Í»§·¢ËÍ´«Õæʱ£¬¾ÍÐèÒªÔÚ¿Í»§¶ËÉϼ¤»îÒ»¸ö·¢ËÍÏûÏ¢µÄ·½·¨£¬Õâ¾ÍºÃ±ÈÎÒÃÇ°´ÁË¡°·¢ËÍ¡±°´Å¥¡£ÏûÏ¢·¢Ë͵½·þÎñ¶Ëºó£¬´¥·¢Ê¼þ£¬Õâ¸öʼþÕýÊÇ·þÎñ¶Ë¶©Ôĵġ£·þÎñ¶Ë»ñµÃ¸ÃʼþÏûÏ¢ºó£¬ÔÙ´¦ÀíÏà¹ØÒµÎñ¡£Õâ¾ÍºÃ±È½ÓÊÕ´«ÕæµÄÈËÔ±£¬µ±´«ÕæÊÕµ½ºó£¬»áÌýµ½½ÓͨµÄÉùÒô£¬´ËʱѡÔñ¡°½ÓÊÕ¡±ºó£¬¸ÃÏûÏ¢¾Í±»²¶»ñÁË¡£

ÏÖÔÚ£¬ÎÒÃǾÍÀ´Ä£ÄâÕâ¸öÁ÷³Ì¡£Ê×Ïȶ¨ÒåÔ¶³Ì¶ÔÏó£¬Õâ¸ö¶ÔÏó´¦ÀíµÄÓ¦¸ÃÊÇÒ»¸ö·¢ËÍ´«ÕæµÄÒµÎñ£º
Ê×ÏÈÊÇÔ¶³Ì¶ÔÏóµÄ¹«¹²½Ó¿Ú£¨Common.dll£©£º

public delegate void FaxEventHandler(string fax);
public interface IFaxBusiness
{
 void SendFax(string fax);
}

×¢Ò⣬ÔÚ¹«¹²½Ó¿Ú³ÌÐò¼¯ÖУ¬¶¨ÒåÁËÒ»¸ö¹«¹²Î¯ÍС£

È»ºóÎÒÃǶ¨Òå¾ßÌå´¦Àí´«ÕæÒµÎñµÄÔ¶³Ì¶ÔÏóÀࣨFaxBusiness.dll£©£¬ÔÚÕâ¸öÀàÖУ¬ÏÈÒªÌí¼Ó¶Ô¹«¹²½Ó¿Ú³ÌÐò¼¯µÄÒýÓãº

public class FaxBusiness:MarshalByRefObject,IFaxBusiness
{ 
 public static event FaxEventHandler FaxSendedEvent;

 #region

 public void SendFax(string fax)
 {
 if (FaxSendedEvent != null)
 {
 FaxSendedEvent(fax);
 }
 }

 #endregion

 public override object InitializeLifetimeService()
 {
 return null;
 }
}

¡¡¡¡Õâ¸öÔ¶³Ì¶ÔÏóÖУ¬Ê¼þµÄÀàÐ;ÍÊÇÎÒÃÇÔÚ¹«¹²³ÌÐò¼¯Common.dllÖж¨ÒåµÄίÍÐÀàÐÍ¡£SendFaxʵÏÖÁ˽ӿÚIFaxBusinessÖеķ½·¨¡£Õâ¸ö·½·¨µÄÇ©ÃûºÍ¶¨ÒåµÄίÍÐÒ»Ö£¬Ëüµ÷ÓÃÁËʼþFaxSendedEvent¡£
ÌØÊâµÄµØ·½ÊÇÎÒÃǶ¨ÒåµÄÔ¶³Ì¶ÔÏó×îºÃÊÇÖØдMarshalByRefObjectÀàµÄInitializeLifetimeService()·½·¨¡£·µ»ØnullÖµ±íÃ÷Õâ¸öÔ¶³Ì¶ÔÏóµÄÉúÃüÖÜÆÚΪÎÞÏÞ´ó¡£ÎªÊ²Ã´ÒªÖØд¸Ã·½·¨ÄØ£¿µÀÀí²»ÑÔ×ÔÃ÷£¬Èç¹ûÉúÃüÖÜÆÚ²»½øÐÐÏÞÖƵĻ°£¬Ò»µ©Ô¶³Ì¶ÔÏóµÄÉúÃüÖÜÆÚ½áÊø£¬Ê¼þ¾ÍÎÞ·¨¼¤»îÁË¡£
½ÓÏÂÀ´¾ÍÊÇ·Ö±ðʵÏÖ¿Í»§¶ËºÍ·þÎñ¶ËÁË¡£·þÎñ¶ËÊÇÒ»¸öWindowsÓ¦ÓóÌÐò£¬½çÃæÈçÏ£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/201605301033455.gif

ÎÒÃÇÔÚ¼ÓÔØ´°ÌåµÄʱºò£¬×¢²áͨµÀºÍÔ¶³Ì¶ÔÏó£º

private void ServerForm_Load(object sender, System.EventArgs e)
{
 HttpChannel channel = new HttpChannel(8080);
 ChannelServices.RegisterChannel(channel);

 RemotingConfiguration.RegisterWellKnownServiceType(
 typeof(FaxBusiness),"FaxBusiness.soap",WellKnownObjectMode.Singleton);
 FaxBusiness.FaxSendedEvent += new FaxEventHandler(OnFaxSended);
}

¡¡¡¡ÎÒÃDzÉÓõÄÊÇSingleTonģʽ£¬×¢²áÁËÒ»¸öÔ¶³Ì¶ÔÏó¡£×¢Òâ¿´£¬Õâ¶Î´úÂëºÍÒ»°ãµÄRemoting·þÎñ¶ËÓÐʲôÇø±ð£¿¶ÔÁË£¬Ëü¶àÁËÒ»ÐÐ×¢²áʼþµÄ´úÂ룺

FaxBusiness.FaxSendedEvent += new FaxEventHandler(OnFaxSended);

ÕâÐдúÂ룬¾ÍºÃ±ÈÎÒÃÇ·þÎñ¶ËµÄ´«Õæ»ú£¬Ò»Ö±Çл»Îª¡°×Ô¶¯¡±Ä£Ê½¡£Ëü»áÒ»Ö±¼àÌý×ÅÀ´×Ô¿Í»§¶ËµÄ´«ÕæÐÅÏ¢£¬Ò»µ©´«ÕæÐÅÏ¢´Ó¿Í»§¶Ë·¢¹ýÀ´ÁË£¬ÔòÏìӦʼþ·½·¨£¬¼´OnFaxSended·½·¨£º

public void OnFaxSended(string fax)
{
 txtFax.Text += fax;
 txtFax.Text += System.Environment.NewLine;
}

Õâ¸ö·½·¨ºÜ¼òµ¥£¬¾ÍÊÇ°Ñ¿Í»§¶Ë·¢¹ýÀ´µÄFaxÏÔʾµ½txtFaxÎı¾¿ò¿Ø¼þÉÏ¡£

¶ø¿Í»§¶ËÄØ£¿ÈÔÈ»ÊÇÒ»¸öWindowsÓ¦ÓóÌÐò¡£´úÂë·Ç³£¼òµ¥£¬Ê×ÏÈΪÁ˼ò±ãÆä¼û£¬ÎÒÃÇÈÔÈ»ÈÃËüÔÚ×°ÔØ´°ÌåµÄʱºò£¬¼¤»îÔ¶³Ì¶ÔÏó£º

private void ClientForm_Load(object sender, System.EventArgs e)
{
 HttpChannel channel = new HttpChannel(0);
 ChannelServices.RegisterChannel(channel);

 faxBus = (IFaxBusiness)Activator.GetObject(typeof(IFaxBusiness),
 "http://localhost:8080/FaxBusiness.soap");
}

ºÇºÇ£¬¿ÉÒÔ˵¿Í»§¶Ë¼¤»î¶ÔÏóµÄ·½·¨ºÍÆÕͨµÄRemoting¿Í»§¶ËÓ¦ÓóÌÐòûÓÐʲô²»Í¬¡£¸Ãд´«ÕæÁË£¡ÎÒÃÇÔÚ´°ÌåÉÏ·ÅÒ»¸öÎı¾¿ò¶ÔÏ󣬸ÄÆäMultilineÊôÐÔΪtrue¡£ÔÙ·ÅÒ»¸ö°´Å¥£¬¸ºÔð·¢ËÍ´«Õ棺

private void btnSend_Click(object sender, System.EventArgs e)
{
 if (txtFax.Text != String.Empty)
 {
 string fax = "À´×Ô" + GetIpAddress() + "¿Í»§¶ËµÄ´«Õæ:"
 + System.Environment.NewLine;
 fax += txtFax.Text;
 faxBus.SendFax(fax);
 }
 else
 {
 MessageBox.Show("ÇëÊäÈë´«ÕæÄÚÈÝ!");
 }
}

private string GetIpAddress()
{ 
 IPHostEntry ipHE = Dns.GetHostByName(Dns.GetHostName());
 return ipHE.AddressList[0].ToString(); 
}

¡¡¡¡ÔÚÕâ¸ö°´Å¥µ¥»÷ʼþÖУ¬Ö»ÐèÒªµ÷ÓÃÔ¶³Ì¶ÔÏófaxBusµÄSendFax()·½·¨¾ÍOKÁË£¬·Ç³£¼òµ¥¡£¿ÉÊÇÂý×Å£¬ÎªÊ²Ã´ÄãµÄ´úÂëÓÐÕâô¶àÐа¡£¿Æäʵ£¬Ã»ÓÐʲôÆæ¹ÖµÄ£¬ÎÒÖ»ÊÇÏëµ½·¢´«ÕæµÄ¿Í»§¿ÉÄÜ»áºÜ¶à¡£ÎªÁ˱ÜÃâ·þÎñ¶ËÈËÔ±·¸ºýÍ¿£¬¸ã²»Çå³þÊÇË­·¢µÄ£¬ËùÒÔÒªÇóÔÚ´«ÕæÉϼÓÉϸ÷×ÔµÄÇ©Ãû£¬Ò²¾ÍÊÇ¿Í»§¶ËµÄIPµØÖ·ÁË¡£¼ÈȻҪ»ñµÃ¼ÆËã»úµÄIPµØÖ·£¬ÇëÒ»¶¨Òª¼ÇµÃ¼ÓÉ϶ÔDNSµÄÃüÃû¿Õ¼äÒýÓãº
using System.Net;

¡¡¡¡ÒòΪÎÒÃÇÑϸñ°´ÕÕ·Ö²¼Ê½´¦Àí³ÌÐòµÄ²¿Êð·½Ê½£¬ËùÒÔÔÚ¿Í»§¶ËÖ»ÐèÒªÌí¼Ó¹«¹²³ÌÐò¼¯(Common.dll)µÄÒýÓþͿÉÒÔÁË¡£¶øÔÚ·þÎñ¶ËÄØ£¬Ôò±ØÐëÌí¼Ó¹«¹²³ÌÐò¼¯ºÍÔ¶³Ì¶ÔÏó³ÌÐò¼¯Á½ÕßµÄÒýÓá£

OK£¬³ÌÐòÍê³É£¬ÎÒÃÇÀ´¿´¿´Õâ¸ö¼òªµÄ´«Õæ»ú£º
¿Í»§¶Ë£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/201605301033456.gif

ºÙºÙ£¬×öÃζ¼Ïë·Å¼Ù°¡¡£ºÃµÄ£¬´«ÕæдºÃÁË£¬·¢ËÍ°É£¡ÔÙ¿´¿´·þÎñ¶Ë£¬great£¬ÀÏ°åÒѾ­ÊÕµ½ÎÒµÄÇë¼ÙÌõ´«ÕæÁË£¡

https://www.jb51.net/files.jb51.net/file_images/article/201605/201605301033457.gif

¶þ¡¢ ¿Í»§¶Ë¶©ÔÄ·þÎñ¶Ëʼþ

¡¡¡¡ºÙºÙ£¬³Ô¸ÊÕáÒªÏȳÔÌðµÄÒ»¶Î£¬×öÊÂÇéÎÒҲϲ»¶ÏÈ×öÈÝÒ׵ġ£ÏÖÔÚ£¬ºÃÈÕ×Ó¹ýÈ¥ÁË£¬¸Ã³Ôµã¿àÍ·ÁË¡£ÎÒÃÇÏÈ»ØÒäһϸղŵÄʵÏÖ·½·¨£¬ÔÙÀ´Ë¼¿¼ÔõôʵÏÖ¿Í»§¶Ë¶©ÔÄ·þÎñ¶Ëʼþ£¿

¡¡¡¡ÔÚÇ°Ò»½Ú£¬Ê¼þ±»·Åµ½Ô¶³Ì¶ÔÏóÖУ¬¿Í»§¶Ë¼¤»î¶ÔÏóºó£¬¾Í¿ÉÒÔ·¢ËÍÏûÏ¢ÁË¡£¶øÔÚ·þÎñ¶Ë£¬Ö»ÐèÒª¶©ÔĸÃʼþ¾Í¿ÉÒÔ¡£ÏÖÔÚ˼·Ӧ¸Ã·´¹ýÀ´£¬ÓÉ¿Í»§¶Ë¶©ÔÄʼþ£¬·þÎñ¶Ë·¢ËÍÏûÏ¢¡£¾ÍÕâô¼òµ¥Âð£¿ÏȲ»Òª¸ßÐ˵ÃÌ«Ôç¡£ÎÒÃÇÏëÒ»Ï룬·¢ËÍÏûÏ¢µÄÈÎÎñÊÇË­À´Íê³ÉµÄ£¿ÊÇÔ¶³Ì¶ÔÏ󡣶øÔ¶³Ì¶ÔÏóÊÇʲôʱºò´´½¨µÄÄØ£¿ÎÒÃÇ×Ðϸ˼¿¼RemotingµÄ¼¸ÖÖ¼¤»î·½Ê½£¬²»¹ÜÊÇ·þÎñ¶Ë¼¤»î£¬»¹ÊÇ¿Í»§¶Ë¼¤»î£¬ËûÃǵŤ×÷Ô­Àí¶¼ÊÇ£º¿Í»§¶Ë¾ö¶¨ÁË·þÎñÆ÷´´½¨Ô¶³Ì¶ÔÏóʵÀýµÄʱ»ú£¬ÀýÈçµ÷ÓÃÁËÔ¶³Ì¶ÔÏóµÄ·½·¨¡£¶ø·þÎñ¶ËËù×÷µÄ¹¤×÷ÔòÊÇ×¢²á¸ÃÔ¶³Ì¶ÔÏó¡£

»ØÒäÕâÈýÖÖ¼¤»î·½Ê½ÔÚ·þÎñ¶ËµÄ´úÂ룺

SingleCall¼¤»î·½Ê½£º

RemotingConfiguration.RegisterWellKnownServiceType(
 typeof(BroadCastObj),"BroadCastMessage.soap",
 WellKnownObjectMode.Singlecall);

SingleTon¼¤»î·½Ê½£º

RemotingConfiguration.RegisterWellKnownServiceType(
 typeof(BroadCastObj),"BroadCastMessage.soap",
 WellKnownObjectMode.Singleton);

¿Í»§¶Ë¼¤»î·½Ê½£º

RemotingConfiguration.ApplicationName = ¡°BroadCastMessage.soap¡±
RemotingConfiguration.RegisterActivatedServiceType(typeof(BroadCastObj));

Çë×¢ÒâRegisterÕâ¸ö´ÊÓËü±í´ïµÄº¬Òå¾ÍÊÇ×¢²á¡£Ò²¾ÍÊÇ˵£¬ÔÚ·þÎñ¶Ë²¢Ã»ÓÐÏÔʾµÄ´´½¨Ô¶³Ì¶ÔÏóʵÀý¡£Ã»ÓиÃʵÀý£¬ÓÖÈçºÎ¹ã²¥ÏûÏ¢ÄØ£¿

»òÐíÓÐÈË»áÏ룬ÔÚ×¢²áÔ¶³Ì¶ÔÏóÖ®ºó£¬ÏÔʽʵÀý¸Ã¶ÔÏ󲻾ͿÉÒÔÁËÂð£¿Ò²¾ÍÊÇ˵£¬ÔÚ×¢²áºó¼ÓÉÏÕâÒ»¶Î´úÂ룺

BroadCastObj obj = new BroadCastObj();

¡¡¡¡È»¶ø£¬ÎÒÃÇÒªÃ÷°×Ò»¸öÊÂʵ£º¾ÍÊÇ·þÎñ¶ËºÍ¿Í»§¶ËÊÇ´¦ÓÚÁ½¸ö²»Í¬µÄÓ¦ÓóÌÐòÓòÖС£Òò´ËÔÚRemotingÖУ¬¿Í»§¶Ë»ñµÃµÄÔ¶³Ì¶ÔÏóʵ¼ÊÊÇ·þÎñ¶Ë×¢²á¶ÔÏóµÄ´úÀí¡£Èç¹ûÎÒÃÇÔÚ×¢²áºó£¬È˹¤È¥´´½¨Ò»¸öʵÀý£¬¶ø·ÇRemotingÔÚ¼¤»îºó×Ô¶¯´´½¨µÄ¶ÔÏó£¬ÄÇô¿Í»§¶Ë»ñµÃµÄ¶ÔÏóÓë·þÎñ¶ËÈ˹¤´´½¨µÄʵÀýÊÇÁ½¸öåÄÈ»²»Í¬µÄ¶ÔÏ󡣿ͻ§¶Ë»ñµÃµÄ´úÀí¶ÔÏó²¢Ã»ÓÐÖ¸ÏòÄã¸Õ²Å´´½¨µÄobjʵÀý¡£ËùÒÔobj·¢Ë͵ÄÏûÏ¢£¬¿Í»§¶Ë¸ù±¾ÎÞ·¨²¶×½¡£

¡¡¡¡ÄÇô£¬ÎÒÃÇÖ»ÓÐÍûÑóÐË̾£¬ÊøÊÖÎÞ²ßÁËÂ𣿱ð׿±£¬±ðÍüÁËÔÚ·þÎñÆ÷×¢²á¶ÔÏó·½·¨ÖУ¬»¹ÓÐÒ»ÖÖ·½·¨£¬¼´Marshal·½·¨°¡¡£»¹¼ÇµÃMarshalµÄʵÏÖ·½Ê½Âð£¿

BroadCastObj Obj = new BroadCastObj();
ObjRef objRef = RemotingServices.Marshal(Obj,"BroadCastMessage.soap");

¡¡¡¡Õâ¸ö·½·¨ÓëÇ°²»Ò»Ñù¡£Ç°ÃæµÄÈýÖÖ·½Ê½£¬Ô¶³Ì¶ÔÏóÊǸù¾Ý¿Í»§¶Ëµ÷Óõķ½Ê½£¬À´×Ô¶¯´´½¨µÄ¡£¶øMarshal·½·¨ÄØ£¿ÔòÏÔʽµØ´´½¨ÁËÔ¶³Ì¶ÔÏóʵÀý£¬È»ºó½«ÆäMarshalµ½Í¨µÀÖУ¬ÐγÉObjRefÖ¸Ïò¶ÔÏóµÄ´úÀí¡£Ö»ÒªÉúÃüÖÜÆÚûÓнáÊø£¬Õâ¸ö¶ÔÏó¾ÍÒ»Ö±´æÔÚ¡£¶ø´Ëʱ¿Í»§¶Ë»ñµÃµÄ¶ÔÏó£¬ÕýÊÇ´´½¨µÄObjʵÀýµÄ´úÀí¡£

OK£¬Õâ¸öÎÊÌâ½â¾öÁË£¬ÎÒÃÇÀ´¿´¿´¾ßÌåʵÏÖ¡£
¹«¹²³ÌÐò¼¯ºÍÔ¶³Ì¶ÔÏóÓëÇ°ÏàËÆ£¬¾Í²»ÔÙ׸Êö£¬Ö»¸½ÉÏ´úÂ룺
¹«¹²³ÌÐò¼¯£º

public delegate void BroadCastEventHandler(string info);

public interface IBroadCast
{
 event BroadCastEventHandler BroadCastEvent;
 void BroadCastingInfo(string info);
}

Ô¶³Ì¶ÔÏóÀࣺ

public event BroadCastEventHandler BroadCastEvent;

#region IBroadCast ³ÉÔ±

//[OneWay]
public void BroadCastingInfo(string info)
{
 if (BroadCastEvent != null)
 {
 BroadCastEvent(info);
 }
}

#endregion

public override object InitializeLifetimeService()
{
 return null;
}

¡¡¡¡ÏÂÃ棬¸ÃʵÏÖ·þÎñ¶ËÁË¡£ÔÚʵÏÖ֮ǰ£¬ÎÒ»¹ÏëÂÞ༸¾ä¡£ÔÚµÚÒ»½ÚÖУ¬ÎÒÃÇʵÏÖÁË·þÎñ¶Ë¶©ÔÄ¿Í»§¶Ëʼþ¡£ÓÉÓÚ¶©ÔÄʼþÊÇÔÚ·þÎñ¶Ë·¢ÉúµÄ£¬Òò´Ëʼþ±¾Éí²¢Î´±»´«ËÍ¡£±»ÐòÁл¯µÄ½ö½öÊÇ´«µÝµÄÏûÏ¢£¬¼´Fax¶øÒÑ¡£ÏÖÔÚ£¬·½Ïò·¢ÉúÁ˸ı䣬´«ËÍÏûÏ¢µÄÊÇ·þÎñ¶Ë£¬¿Í»§¶Ë¶©ÔÄÁËʼþ¡£µ«Õâ¸öʼþÊÇ·ÅÔÚÔ¶³Ì¶ÔÏóÖеģ¬Òò´Ëʼþ±ØÐë±»ÐòÁл¯¡£¶øÔÚ.Net Framework1.1ÖУ¬Î¢Èí¶ÔÐòÁл¯µÄ°²È«¼¶±ð½øÐÐÁËÏÞÖÆ¡£ÓйØίÍкÍʼþµÄÐòÁл¯¡¢·´ÐòÁл¯Ä¬ÈÏÊǽûÖ¹µÄ£¬ËùÒÔÎÒÃÇÓ¦¸Ã½«TypeFilterLevelµÄÊôÐÔÖµÉèÖÃΪFullö¾ÙÖµ¡£Òò´ËÔÚ·þÎñ¶Ë×¢²áͨµÀµÄ·½Ê½¾Í·¢ÉúÁ˸ı䣺

private void StartServer()
{
 BinaryServerFormatterSinkProvider serverProvider = new
 BinaryServerFormatterSinkProvider();
 BinaryClientFormatterSinkProvider clientProvider = new
 BinaryClientFormatterSinkProvider();
 serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

 IDictionary props = new Hashtable();
 props["port"] = 8080;
 HttpChannel channel = new HttpChannel(props,clientProvider,serverProvider);
 ChannelServices.RegisterChannel(channel);

 Obj = new BroadCastObj();
 ObjRef objRef = RemotingServices.Marshal(Obj,"BroadCastMessage.soap"); 
}

×¢ÒâÓï¾äserverProvider.TypeFilterLevel = TypeFilterLevel.Full£»´ËÓï¾ä¼´ÉèÖÃÐòÁл¯°²È«¼¶±ðµÄ¡£ÒªÊ¹ÓÃTypeFilterLevelÊôÐÔ£¬±ØÐëÉêÃ÷ÃüÃû¿Õ¼ä£º
using System.Runtime.Serialization.Formatters;

¶øºóÃæÁ½ÌõÓï¾ä¾ÍÊÇ×¢²áÔ¶³Ì¶ÔÏó¡£ÓÉÓÚÔÚÎҵĹ㲥³ÌÐòÖУ¬·¢Ë͹㲥ÏûÏ¢ÊÇ·ÅÔÚÁíÒ»¸ö´°¿ÚÖУ¬Òò´ËÎÒ½«¸ÃÔ¶³Ì¶ÔÏóÉùÃ÷Ϊ¹«¹²¾²Ì¬¶ÔÏó£º

public static BroadCastObj Obj = null;

È»ºóÔÚµ÷Óô°¿ÚʼþÖмÓÈ룺

private void ServerForm_Load(object sender, System.EventArgs e)
{
 StartServer();
 lbMonitor.Items.Add("Server started!");
}

À´¿´¿´½çÃ棬Ê×ÏÈÆô¶¯·þÎñ¶ËÖ÷´°¿Ú£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/201605301033458.gif

ÎÒ·ÅÁËÒ»¸öListBox¿Ø¼þÀ´ÏÔʾһЩÐÅÏ¢£¬ÀýÈçÏÔʾ·þÎñÆ÷Æô¶¯ÁË¡£¶øBroadCast°´Å¥¾ÍÊǹ㲥ÏûÏ¢µÄ£¬µ¥»÷¸Ã°´Å¥£¬»áµ¯³öÒ»¸ö¶Ô»°¿ò£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/201605301033459.gif

BraodCast°´Å¥µÄ´úÂ룺

private void btnBC_Click(object sender, System.EventArgs e)
{ 
 BroadCastForm bcForm = new BroadCastForm();
 bcForm.StartPosition = FormStartPosition.CenterParent;
 bcForm.ShowDialog();
}

ÔÚ¶Ô»°¿òÖУ¬×îÖ÷ÒªµÄ¾ÍÊÇSend°´Å¥£º

if (txtInfo.Text != string.Empty)
{ 
 ServerForm.Obj.BroadCastingInfo(txtInfo.Text);
}
else
{
 MessageBox.Show("ÇëÊäÈëÐÅÏ¢£¡");
}


µ«ÊǺܼòµ¥£¬¾ÍÊǵ÷ÓÃÔ¶³Ì¶ÔÏóµÄ·¢ËÍÏûÏ¢·½·¨¶øÒÑ¡£

ÏÖÔÚ¸ÃʵÏÖ¿Í»§¶ËÁË¡£ÎÒÃÇ¿ÉÒÔ²ÎÕÕÇ°ÃæµÄÀý×Ó£¬Ö»ÊÇ°Ñ·þÎñ¶Ë¸ÄΪ¿Í»§¶Ë¶øÒÑ¡£ÁíÍ⿼Âǵ½ÐòÁл¯°²È«¼¶±ðµÄÎÊÌ⣬ËùÒÔ´úÂë»áÊÇÕâÑù£º

private void ClientForm_Load(object sender, System.EventArgs e)
{
 BinaryServerFormatterSinkProvider serverProvider = new
 BinaryServerFormatterSinkProvider();
 BinaryClientFormatterSinkProvider clientProvider = new
 BinaryClientFormatterSinkProvider();
 serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

 IDictionary props = new Hashtable();
 props["port"] = 0;
 HttpChannel channel = new HttpChannel(props,clientProvider,serverProvider);
 ChannelServices.RegisterChannel(channel);

 watch = (IBroadCast)Activator.GetObject(
 typeof(IBroadCast),"http://localhost:8080/BroadCastMessage.soap"); 
 watch.BroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);
}

×¢Òâ¿Í»§¶ËͨµÀµÄ¶Ë¿ÚºÅÓ¦ÉèÖÃΪ0£¬Õâ±íʾ¿Í»§¶Ë×Ô¶¯Ñ¡Ôñ¿ÉÓõĶ˿ںš£Èç¹ûÒªÉèÖÃΪָ¶¨µÄ¶Ë¿ÚºÅ£¬Ôò±ØÐë±£Ö¤Óë·þÎñ¶ËͨµÀµÄ¶Ë¿ÚºÅ²»Ïàͬ¡£
È»ºóÊÇ£¬BroadCastEventHandlerίÍеķ½·¨£º

public void BroadCastingMessage(string message)
{
 txtMessage.Text += "I got it:" + message; 
 txtMessage.Text += System.Environment.NewLine; 
}

¿Í»§¶Ë½çÃæÈçͼ£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334510.gif

ºÃ£¬ÏÂÃæÈÃÎÒÃÇÂú»³ÆÚÅΣ¬À´ÔËÐÐÕâ¶Î³ÌÐò¡£Ê×ÏÈÆô¶¯·þÎñ¶ËÓ¦ÓóÌÐò£¬È»ºóÆô¶¯¿Í»§¶Ë¡£°¥Ñ½£¬Ôã¸â£¬¾ÓÈ»³öÏÖÁË´íÎóÐÅÏ¢£¡

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334511.gif

¡¡¡¡¡°ÈËÖ®²»ÈçÒâÊ£¬Ê®³£¾Ó°Ë¾Å¡£¡±²»ÓþÚÉ¥£¬ÈÃÎÒÃÇ·ÖÎöÔ­Òò¡£Ê×ÏÈ¿´¿´´íÎóÐÅÏ¢£¬Ëü±¨¸æÎÒÃÇûÓÐÕÒµ½Client³ÌÐò¼¯¡£È»¶øÊÂʵÉÏ£¬Client³ÌÐò¼¯µ±È»ÊÇÓеġ£ÄÇôÔÙÀ´µ÷ÊÔһϣ¬ÊÇÄÄÒ»²½³öÏÖµÄÎÊÌâÄØ£¿ÉèÖúöϵ㣬½øÐÐÖðÓï¾ä¸ú×Ù¡£Ç°Ãæ×¢²áͨµÀÒ»ÇÐÕý³££¬µ±ÔËÐе½watch.BroadCastEvent += new BroadCastEventHandler(BroadCastingMessage)Óï¾äʱ£¬´íÎó³öÏÖÁË£¡

¡¡¡¡Ò²¾ÍÊÇ˵£¬Ô¶³Ì¶ÔÏóµÄ´´½¨Êdzɹ¦µÄ£¬µ«ÔÚ¶©ÔÄʼþµÄʱºòʧ°ÜÁË¡£Ô­ÒòÊÇʲôÄØ£¿Ô­À´£¬¿Í»§¶ËµÄίÍÐÊÇͨ¹ýÐòÁл¯ºó»ñµÃµÄ£¬ÔÚ¶©ÔÄʼþµÄʱºò£¬Î¯ÍÐÊÔͼװÔØ°üº¬ÓëÇ©ÃûÏàͬµÄ·½·¨µÄ³ÌÐò¼¯£¬Ò²¾ÍÊÇBroadCastingMessage·½·¨ËùÔڵijÌÐò¼¯Client¡£È»¶øÕâ¸ö×°ÔصĹý³Ì·¢ÉúÔÚ·þÎñ¶Ë£¬¶øÔÚ·þÎñ¶Ë£¬²¢Ã»ÓÐClient³ÌÐò¼¯´æÔÚ£¬×ÔÈ»¾Í·¢ÉúÁËÉÏÃæµÄÒì³£¡£

¡¡¡¡Ô­ÒòÇå³þÁË£¬Ôõô½â¾ö£¿Ê×ÏÈBroadCastingMessage·½·¨¿Ï¶¨ÊÇÔÚ¿Í»§¶ËÖУ¬ËùÒÔ²»¿É±ÜÃ⣬ίÍÐ×°ÔØClient³ÌÐò¼¯µÄ¹ý³ÌÒ²±ØÐëÔÚ¿Í»§¶ËÍê³É¡£¶ø·þÎñ¶ËʼþÓÖÊÇÓÉÔ¶³Ì¶ÔÏóÀ´²¶»ñµÄ£¬Òò´Ë£¬ÔÚ¿Í»§¶Ë×¢²áµÄÒ²¾Í±ØÐëÊÇÔ¶³Ì¶ÔÏóʼþÁË¡£Ò»¸öÒªÇó±ØÐëÔÚ¿Í»§¶Ë£¬Ò»¸öÓÖÒªÇó±ØÐëÔÚ·þÎñ¶Ë£¬ÊÂÇé³öÏÖÁË×ÔÏàì¶ÜµÄµØ·½¡£

ÄÇô£¬ÈÃÎÒÃÇÏÈÏëÏëÕâÑùÒ»¸öÀý×Ó¡£¼ÙÉèÎÒÃÇÒª½»»»xºÍyµÄÖµ£¬¸ÃÕâÑùÍê³É£¿ºÜ¼òµ¥£¬ÒýÈëÒ»¸öÖмä±äÁ¿¾Í¿ÉÒÔÁË¡£

int x=1,y=2,z;
z = x;
x = y;
y = z;

Õâ¸öÓÎÏ·ÏàÐÅ´ó¼Ò¶¼»áÍæ°É£¬ÄÇôºÃµÄ£¬ÎÒÃÇÒ²ÐèÒªÒýÈëÕâÑùÒ»¸ö¡°Öм䡱¶ÔÏó¡£Õâ¸öÖмä¶ÔÏóºÍÔ­À´µÄÔ¶³Ì¶ÔÏóÔÚʼþ´¦Àí·½Ã棬´úÂëÍêÈ«Ò»Ö£º

public class EventWrapper:MarshalByRefObject
{
 public event BroadCastEventHandler LocalBroadCastEvent;

 //[OneWay]
 public void BroadCasting(string message)
 {
 LocalBroadCastEvent(message);
 }

 public override object InitializeLifetimeService()
 {
 return null;
 }
}

²»¹ý²»Í¬Ö®´¦ÔÚÓÚ£ºÕâ¸öWrapperÀà±ØÐëÔÚ¿Í»§¶ËºÍ·þÎñ¶ËÉ϶¼Òª²¿Êð£¬ËùÒÔ£¬Õâ¸öÀàÓ¦¸Ã·ÅÔÚ¹«¹²³ÌÐò¼¯Common.dllÖС£

ÏÖÔÚÔÙÀ´ÐÞ¸ÄÔ­À´µÄ¿Í»§¶Ë´úÂ룺

watch = (IBroadCast)Activator.GetObject(
 typeof(IBroadCast),"http://localhost:8080/BroadCastMessage.soap"); 
watch.BroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);

ÐÞ¸ÄΪ£º

watch = (IBroadCast)Activator.GetObject(
 typeof(IBroadCast),"http://localhost:8080/BroadCastMessage.soap");
EventWrapper wrapper = new EventWrapper(); 
wrapper.LocalBroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);
watch.BroadCastEvent += new BroadCastEventHandler(wrapper.BroadCasting);

ΪʲôÕâÑù×ö¾Í¿ÉÒÔÁËÄØ£¿Ò²Ðí»­Ò»·ùͼ¾ÍºÜÈÝÒ×˵Ã÷£¬¿ÉϧÎÒµÄÒÕÊõÌì·ÖʵÔÚºÜÔã¸â£¬ÎÒÏ£ÍûÒÔºó¿ÉÒԸĽøÕâÒ»µã¡£»¹ÊÇÓÃÎÄ×ÖÀ´ËµÃ÷°É¡£

Ç°Ãæ˵£¬Î¯ÍÐҪװÔØclient³ÌÐò¼¯¡£ÏÖÔÚÎÒÃÇ°ÑÔ¶³Ì¶ÔÏóίÍÐ×°ÔصÄȨÀûÒƽ»¸øEventWrapper¡£ÒòΪÕâ¸öÀà¶ÔÏóÊÇ·ÅÔÚ¿Í»§¶ËµÄ£¬ËùÒÔËüҪװÔØclient³ÌÐò¼¯Ë¿ºÁûÓÐÎÊÌâ¡£Óï¾ä£º

EventWrapper wrapper = new EventWrapper(); 
wrapper.LocalBroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);

ʵÏÖÁËÕâ¸ö¹¦ÄÜ¡£

¡¡¡¡²»¹ý´ËʱËäÈ»¶©ÔÄÁËʼþ£¬µ«Ê¼þ»¹ÊÇ¿Í»§¶ËµÄ£¬Ã»ÓÐÓë·þÎñ¶ËÁªÏµÆðÀ´¡£¶ø·þÎñ¶ËµÄʼþÊǷŵ½Ô¶³Ì¶ÔÏóÖеģ¬ËùÒÔ£¬»¹Òª¶©ÔÄʼþ£¬Õâ¸öÈÎÎñÓÉÔ¶³Ì¶ÔÏówatchÀ´Íê³É¡£µ«´ËʱËü¶©ÔĵIJ»ÔÙÊÇBroadCastingMessageÁË£¬¶øÊÇEventWrapperµÄ´¥·¢Ê¼þ·½·¨BroadCasting¡£ÄÇô´ËʱίÍÐͬÑùҪװÔسÌÐò¼¯£¬µ«´ËʱװÔصľÍÊÇBroadCastingËùÔڵijÌÐò¼¯ÁË¡£ÓÉÓÚ×°ÔØ·¢ÉúµÄµØµãÊÇÔÚ·þÎñ¶Ë¡£ºÇºÇ£¬¸ßÐ˵ÄÊÇ£¬BroadCastingËùÔڵijÌÐò¼¯ÕýÊǹ«¹²³ÌÐò¼¯£¨Ç°ÃæÒÑ˵¹ý£¬EventWrapperÓ¦·Åµ½¹«¹²³ÌÐò¼¯Common.dllÖУ©£¬¶ø¹«¹²³ÌÐò¼¯ÔÚ·þÎñ¶ËºÍ¿Í»§¶Ë¶¼ÒѾ­²¿ÊðÁË¡£×ÔÈ»¾Í²»»á³öÏÖÕÒ²»µ½³ÌÐò¼¯µÄÎÊÌâÁË¡£

×¢Ò⣺EventWrapperÒòΪҪÖØдInitializeLifetimeService()·½·¨£¬ËùÒÔÈÔȻҪ¼Ì³ÐMarshalByRefObjectÀà¡£

ÏÖÔÚÔÙÀ´ÔËÐгÌÐò¡£Ê×ÏÈÔËÐзþÎñ¶Ë£»È»ºóÔËÐпͻ§¶Ë£¬OK£¬¿Í»§¶Ë´°Ìå³öÏÖÁË£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334510.gif

È»ºóÎÒÃÇÔÚ·þÎñ¶Ëµ¥»÷¡°BroadCast¡±°´Å¥£¬·¢Ë͹㲥ÏûÏ¢£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334512.gif

µ¥»÷¡°Send¡±·¢ËÍ£¬ÔÙÀ´¿´¿´¿Í»§¶Ë£¬»áÊÇÔõÑù£¿Fine£¬I got it!

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334513.gif

ÔõôÑù£¬ºÜ¿á°É£¡ÄãÒ²¿ÉÒÔͬʱ´ò¿ª¶à¸ö¿Í»§¶Ë£¬ËüÃǶ¼½«ÊÕµ½Õâ¸ö¹ã²¥ÐÅÏ¢¡£Èç¹ûÄã¾õµÃÕâ¸ö¹ã²¥ÉùÒôÌ«³³£¬ÄǾÍÇëÄãÔÚ¿Í»§¶ËÈ¡Ïû¹ã²¥°É¡£ÔÚCancle°´Å¥ÖУº

private void btnCancle_Click(object sender, System.EventArgs e)
{
 watch.BroadCastEvent -= new BroadCastEventHandler(wrapper.BroadCasting);
 MessageBox.Show("È¡Ïû¶©ÔĹ㲥³É¹¦!");
}

µ±È»Õâ¸öʱºòwrapper¶ÔÏóÓ¦¸Ã±»ÉêÃ÷Ϊprivate¶ÔÏóÁË£º
private EventWrapper wrapper = null;

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334514.gif

È¡Ïûºó£¬ÄãÊÔ×ÅÔٹ㲥һÏ£¬¹§Ï²Ä㣬Äã²»»áÌýµ½ÔëÒôÁË£¡

Èý¡¢ ¿Í»§¶Ë¶©ÔÄ¿Í»§¶Ëʼþ

¡¡¡¡ÓÐÁËÇ°ÃæµÄ»ù´¡£¬ÔÙÀ´¿´¿Í»§¶Ë¶©ÔÄ¿Í»§¶Ëʼþ£¬¾Í¼òµ¥¶àÁË¡£¶ø±¾ÎÄдµ½ÕâÀÎÒÒ²ºÜÀÛÁË£¬ÄãÒ²±»ÎÒ†ªàµò»ÄÍ·³ÁË¡£ÄãÐÄÀïÔÚº°£¬¡°ÈÄÁËÎÒ°É£¡¡±Æäʵ£¬ÎÒÓֺγ¢²»ÊÇÈç´Ë¡£ËùÒÔÎÒÖ»Ìṩһ¸ö˼·£¬ÓÐÐËȤµÄÅóÓÑ£¬¿ÉÒÔ×Ô¼ºÐ´Ò»¸ö³ÌÐò¡£

¡¡¡¡Æäʵ·½·¨ºÜ¼òµ¥£¬ºÍµÚ¶þÖÖÇé¿öÀàËÆ¡£·¢ËÍÐÅÏ¢µÄ¿Í»§¶Ë£¬Ö»ÐèÒª»ñµÃÔ¶³Ì¶ÔÏóºó£¬·¢ËÍÏûÏ¢¾Í¿ÉÒÔÁË¡£¶ø½ÓÊÕÐÅÏ¢µÄ¿Í»§¶Ë£¬¸ºÔð¶©ÔĸÃʼþ¡£ÓÉÓÚʼþ¶¼ÊǷŵ½Ô¶³Ì¶ÔÏóÖУ¬Òò´Ë¶©Ôĵķ½·¨ºÍµÚ¶þÖÖÇé¿öûÓÐʲôÇø±ð£¡

¡¡¡¡ÌØÊâµÄÇé¿öÊÇ£¬ÎÒÃÇ¿ÉÒÔÓõÚÈýÖÖÇé¿öÀ´´úÌæµÚ¶þÖÖ¡£Ö»ÒªÄã°Ñ·¢ËÍÐÅÏ¢µÄ¿Í»§¶Ë·Åµ½·þÎñ¶Ë¾Í¿ÉÒÔÁË¡£µ±È»ÐèÒª×öһЩ¶îÍâµÄ¹¤×÷£¬ÓÐÐËȤµÄÅóÓÑ¿ÉÒÔȥʵÏÖһϡ£ÔÚÎÒµÄʾÀý³ÌÐòÖУ¬ÒѾ­ÓÃÕâÖÖ·½·¨Ä£ÄâʵÏÖÁË·þÎñ¶ËµÄ¹ã²¥£¬´ó¼Ò¿ÉÒÔÈ¥¿´¿´¡£

ËÄ¡¢ Ò»µã²¹³ä

ÎÒÔÚÇ°ÃæµÄʼþ´¦ÀíÖУ¬Ê¹ÓõĶ¼ÊÇĬÈϵÄEventArgs¡£Èç¹ûÒª¶¨Òå×Ô¼ºµÄEventArgs£¬¾Í²»ÏàͬÁË¡£ÒòΪ¸ÃÐÅÏ¢ÊÇ´«ÖµÐòÁл¯£¬Òò´Ë±ØÐë¼ÓÉÏ[Serializable]£¬ÇÒ±ØÐë·Åµ½¹«¹²³ÌÐò¼¯ÖУ¬²¿Êðµ½·þÎñ¶ËºÍ¿Í»§¶Ë¡£ÀýÈ磺

[Serializable]
public class BroadcastEventArgs:EventArgs
{
 private string msg = null;
 public BroadcastEventArgs(string message)
 {
 msg = message;
 }

 public string Message
 {
 get {return msg;}
 }
}

Îå¡¢³ÖÐø¸Ä½ø£¨¾­BetaµÄÌáÐÑ£¬ÎҸĽøÁËÎҵijÌÐò£¬²¢¶ÔÎÄÕ½øÐÐÁËÐÞ¸Ä 2004Äê12ÔÂ13ÈÕ£©

¡¡¡¡Ò²Ðí£¬Ï¸ÐĵĶÁÕß×¢Òâµ½ÁË£¬ÔÚÎÒµÄÔ¶³Ì¶ÔÏóÀàºÍEventWrapperÀàÖУ¬´¥·¢Ê¼þ·½·¨µÄAttribute[OneWay]±»ÎÒ×¢Ê͵ôÁË¡£ÎÒ¿´µ½ºÜ¶à×ÊÁÏÉÏдµ½£¬ÔÚRemotingÖд¦Àíʼþ£¬´¥·¢Ê¼þµÄ·½·¨±ØÐë¾ßÓÐÕâ¸öAttribute¡£Õâ¸öattribute¾¿¾¹ÓÐʲôÓã¿

¡¡¡¡ÔÚ·¢ËÍʼþÏûÏ¢µÄʱºò£¬Ê¼þµÄ¶©ÔÄÕ߻ᴥ·¢Ê¼þ£¬È»ºóÏìÓ¦¸Ãʼþ¡£È»¶øµ±Ê¼þµÄ¶©ÔÄÕß·¢Éú´íÎóµÄʱºòÄØ£¿ÀýÈ磬·¢ËÍʼþÏûÏ¢µÄʱºò£¬²Å·¢ÏÖ¸ù±¾Ã»ÓÐʼþ¶©ÔÄÕߣ»»òÕßʼþµÄ¶©ÔÄÕß³öÏÖ¹ÊÕÏ£¬Èç¶Ïµç¡¢»òÒì³£¹Ø»ú¡£´Ëʱ£¬·¢ËÍʼþÒ»·½»áÒòΪÕÒ²»µ½ÕýÈ·µÄʼþ¶©ÔÄÕߣ¬¶ø·¢ÉúÒì³£¡£ÒÔÎҵijÌÐòΪÀý¡£µ±ÎÒÃÇ·Ö±ð´ò¿ª·þÎñ¶ËºÍ¿Í»§¶Ë³ÌÐòµÄʱºò£¬´Ëʱ¹ã²¥ÐÅÏ¢Õý³£¡£È»¶ø£¬µ±ÎÒÃǹرտͻ§¶Ëºó£¬ÓÉÓڸÿͻ§¶ËûÓÐÈ¡Ïû¶©ÔÄ£¬´ËʱÒì³£·¢Éú£¬ÌáʾÐÅÏ¢Èçͼ£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334515.gif

¡¡¡¡£¨²»ÖªµÀΪʲô£¬Õâ¸öÒì³£Óë¿Í»§¶ËÁ¬½Ó·þÎñ¶Ë³öÏÖµÄÒì³£Ò»Ñù¡£Õâ¸öÒì³£ÈÝÒ×ÈÃÈ˲úÉúÎó»á¡££©

¡¡¡¡Èç¹ûÕâ¸öʱºòÎÒÃÇͬʱ´ò¿ªÁ˶à¸ö¿Í»§¶Ë£¬ÄÇôÆäËû¿Í»§¶Ë¾Í»áÒòΪÕâÒ»¸ö¿Í»§¶Ë¹Ø±ÕÔì³ÉµÄ´íÎ󣬶øÎÞ·¨ÊÕµ½¹ã²¥ÐÅÏ¢¡£ÄÇôÈÃÎÒÃÇÏÈ×öµÚÒ»²½¸Ä½ø£º

1¡¢ÏÈ¿¼ÂÇÕý³£Çé¿ö¡£ÔÚÎҵĿͻ§¶Ë£¬ËäÈ»ÌṩÁËÈ¡Ïû¶©ÔĵIJÙ×÷£¬µ«²¢Ã»Óп¼ÂÇÓû§¹Ø±Õ¿Í»§¶ËµÄÇé¿ö¡£¼´£¬¹Ø±Õ¿Í»§¶Ëʱ£¬²¢Î´È¡ÏûʼþµÄ¶©ÔÄ£¬ËùÒÔÎÒÃÇÓ¦¸ÃÔڹرտͻ§¶Ë´°ÌåÖÐдÈ룺

private void ClientForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
 watch.BroadCastEvent -= new BroadCastEventHandler(wrapper.BroadCasting);
}

2¡¢½ö½öÊÇÕâÑù»¹²»¹»¡£Èç¹û¿Í»§¶Ë²¢Ã»ÓÐÕý³£¹Ø±Õ£¬¶øÊÇÒòΪͻȻ¶Ïµç¶øµ¼Ö¿ͻ§¶Ë¹Ø±ÕÄØ£¿´Ëʱ£¬¿Í»§¶Ë»¹Ã»ÓÐÀ´µÃ¼°È¡Ïûʼþ¶©ÔÄÄØ¡£ÔÚÕâÖÖÇé¿öÏ£¬ÎÒÃÇÐèÒªÓõ½OneWayAttribute¡£

Ç°Ãæ˵µ½£¬·¢ËÍʼþÒ»·½Èç¹ûÕÒ²»µ½ÕýÈ·µÄʼþ¶©ÔÄÕߣ¬»á·¢ÉúÒì³£¡£Ò²¾ÍÊÇ˵£¬Õâ¸öʼþÊÇunreachableµÄ¡£ÐÒÔ˵ÄÊÇ£¬OneWayAttributeÇ¡ºÃ½â¾öÁËÕâ¸öÎÊÌâ¡£Æäʵ´Ó¸ÃÌØÐÔµÄÃüÃûOneWay£¬´óÔ¼Ò²Äܲµ½ÆäÖеĺ¬Òå¡£µ±Ê¼þ²»¿Éµ½´ï£¬ÎÞ·¨·¢ËÍʱ£¬Õý³£Çé¿öÏ£¬»á·µ»ØÒ»¸öÒì³£ÐÅÏ¢¡£Èç¹û¼ÓÉÏOneWayAttribute£¬Õâ¸öʼþµÄ·¢Ë;ͱä³Éµ¥ÏòµÄÁË¡£¼ÙÈç´Ëʱ·¢ÉúÒì³££¬ÄÇôϵͳ»á×Ô¶¯Å×µô¸ÃÒì³£ÐÅÏ¢¡£ÓÉÓÚûÓÐÒì³£ÐÅÏ¢µÄ·µ»Ø£¬·¢ËÍÐÅÏ¢·½»áÈÏΪ·¢ËÍÐÅÏ¢³É¹¦ÁË¡£³ÌÐò»áÕý³£ÔËÐУ¬´íÎóµÄ¿Í»§¶Ë±»ºöÂÔ£¬¶øÕýÈ·µÄ¿Í»§¶ËÈÔÈ»Äܹ»ÊÕµ½¹ã²¥ÐÅÏ¢¡£

Òò´Ë£¬Ô¶³Ì¶ÔÏóµÄ´úÂë¾ÍÓ¦¸ÃÊÇÕâÑù£º

public event BroadCastEventHandler BroadCastEvent;

IBroadCast ³ÉÔ±

public override object InitializeLifetimeService()
{
 return null;
}

3¡¢×îºóµÄ¸Ä½ø

¡¡¡¡Ê¹ÓÃOneWay¹ÌÈ»¿ÉÒÔ½â¾öÉÏÊöµÄÎÊÌ⣬µ«²»¹»ÓѺá£ÒòΪ¶ÔÓڹ㲥ÏûÏ¢µÄÒ»·½À´Ëµ£¬Ïó±»ÃÉÉÏÁËÑÛ¾¦Ò»Ñù£¬¶ÔÓÚ¿Í»§¶Ë·¢ÉúµÄÊÂÇéãÂÈ»²»Öª¡£Õâ²¢²»ÊÇÒ»¸öºÃµÄidea¡£ÔÚIngo RammerµÄAdvanced .NET RemotingÒ»ÊéÖУ¬Ingo RammerÏÈÉúÌá³öÁËÒ»¸ö¸üºÃµÄ°ì·¨£¬¾ÍÊÇÔÚ·¢ËÍÐÅÏ¢Ò»·½Ê±£¬¼ì²éÁËίÍÐÁ´¡£²¢ÔÚίÍÐÁ´µÄ±éÀúÖÐÀ´²¶»ñÒì³£¡£µ±ÆäÖÐÒ»¸öίÍз¢ÉúÒ쳣ʱ£¬ÏÔʾÌáʾÐÅÏ¢¡£È»ºó¼ÌÐø±éÀúºóÃæµÄίÍУ¬ÕâÑù¼È±£Ö¤ÁËÒì³£ÐÅÏ¢µÄÌáʾ£¬ÓÖ±£Ö¤ÁËÆäËû¶©ÔÄÕßÕý³£½ÓÊÕÏûÏ¢¡£Òò´Ë£¬ÎÒ¶Ô±¾ÀýµÄÔ¶³Ì¶ÔÏó½øÐÐÁËÐ޸ģ¬×¢Ê͵ô[OneWay]£¬ÐÞ¸ÄÁËBroadCastInfo()·½·¨£º

//[OneWay]
public void BroadCastingInfo(string info)
{
 if (BroadCastEvent != null)
 {
 BroadCastEventHandler tempEvent = null;

 int index = 1; //¼Ç¼Ê¼þ¶©ÔÄÕßίÍеÄË÷Òý£¬Îª·½±ã±êʶ£¬´Ó1¿ªÊ¼¡£
 foreach (Delegate del in BroadCastEvent.GetInvocationList())
 {
 try
 {
 tempEvent = (BroadCastEventHandler)del;
 tempEvent(info);
 }
 catch
 { 
 MessageBox.Show("ʼþ¶©ÔÄÕß" + index.ToString() + "·¢Éú´íÎó,ϵͳ½«È¡Ïûʼþ¶©ÔÄ!");
 BroadCastEvent -= tempEvent;
 }
 index++;
 } 
 }
 else
 {
 MessageBox.Show("ʼþδ±»¶©ÔÄ»ò¶©ÔÄ·¢Éú´íÎó!");
 }
}

ÎÒÃÇÀ´ÊÔÑéһϡ£Ê×ÏÈ´ò¿ª·þÎñ¶Ë£¬È»ºóͬʱ´ò¿ªÈý¸ö¿Í»§¶Ë¡£¹ã²¥ÏûÏ¢£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334525.gif

ÏûÏ¢·¢ËÍÕý³£¡£

½Ó׏رÕÆäÖÐÒ»¸ö¿Í»§¶Ë´°¿Ú£¬Ôٹ㲥ÏûÏ¢£¨×¢ÒâΪģÄâ¿Í»§¶ËÒì³£Çé¿ö£¬Ó¦ÔÚClientForm_Closing·½·¨ÖаѵÚÒ»²½¸Ä½øµÄÈ¡Ïû¶©ÔÄ´úÂë×¢ÊÍ¡£·ñÔò²»»á·¢ÉúÒì³£¡£ÄѵÀÄãÕæµÄÔ¸ÒâÓöϵçÀ´µ¼ÖÂÒì³£·¢ÉúÂð^_^£©£¬½á¹ûÈçͼ£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334526.gif

´Ëʱ·þÎñ¶Ë±¨¸æÁË¡°Ê¼þ¶©ÔÄÕß1·¢Éú´íÎó£¬ÏµÍ³½«È¡Ïûʼþ¶©ÔÄ¡±¡£×¢Òâ´ËʱÁíÍâÁ½¸ö¿Í»§¶Ë£¬»¹ÊǺÍÇ°ÃæÒ»Ñù£¬Ö»ÓÐÁ½Ìõ¹ã²¥ÐÅÏ¢¡£

µ±ÎÒÃǵã»÷Ìáʾ¿òµÄ¡°È·¶¨¡±°´Å¥ºó£¬¹ã²¥ÈÔÈ»·¢ËÍ£º

https://www.jb51.net/files.jb51.net/file_images/article/201605/2016053010334527.gif

ͨ¹ýÕâÑùµÄ¸Ä½øºó£¬³ÌÐò¸ü¼ÓµÄÍêÉÆ£¬Ò²¸ü¼ÓµÄ½¡×³ºÍÓѺã¡

¸½£º
ʾÀý´úÂë˵Ã÷£º
1¡¢Remotingʼþ(¿Í»§¶Ë·¢´«Õæ)ѹËõ°ü£ºÎªµÚÒ»½ÚÄÚÈÝ£»
2¡¢Remotingʼþ(·þÎñ¶Ë¹ã²¥)ѹËõ°ü£ºÎªµÚ¶þ½Ú¡¢µÚÈý½ÚÄÚÈÝ£¬ÆäÖУº
µÚ¶þ½Ú´úÂë°üº¬ÓÚ£º
#region ¿Í»§¶Ë¶©ÔÄ·þÎñ¶Ëʼþ
#endregion
µÚÈý½Ú´úÂë°üº¬ÓÚ£º
#region ¿Í»§¶Ë¶©ÔÄ¿Í»§¶Ëʼþ
#endregion
Èç¹ûҪʵÏÖµÚ¶þ½ÚµÄ³ÌÐò£¬Çë×¢Ê͵ôµÚÈý½Ú´úÂ룻·´Ö®ÒàÈ»¡£Ê¾Àý³ÌÐòĬÈÏΪµÚ¶þ½Ú³ÌÐò¡£
3¡¢ÔËÐÐʾÀý³ÌÐòʱ£¬ÇëÏÈÔËÐзþÎñ¶Ë³ÌÐò£¬È»ºóÔËÐпͻ§¶Ë³ÌÐò¡£·ñÔò»áÅ׳ö¡°»ù´¡Á¬½ÓÒѹرա±µÄÒì³£¡£
4¡¢½â¾ö·½°¸¾ù·ÅÔÚCommon£¨»òICommon£©Îļþ¼ÐÖС£

5¡¢¸Ä½øºóµÄ´úÂë·Åµ½Remotingʼþ(·þÎñ¶Ë¹ã²¥¸Ä½ø)ѹËõ°üÖУ¬´ó¼Ò¿ÉÒԱȽÏһϸĽøºóµÄ³ÌÐòÓкβ»Í¬£¡

ÒÔÉϾÍÊÇ.Net RemotingÖÐRemotingʼþ´¦ÀíµÄÈ«²¿ÄÚÈÝ£¬Ï£ÍûÄܸø´ó¼ÒÒ»¸ö²Î¿¼£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö½Å±¾Ö®¼Ò¡£

»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

2

Ö÷Ìâ

2Íò

»ØÌû

347

»ý·Ö

Ö춻áÔ±

Rank: 3Rank: 3

»ý·Ö
347
·¢±íÓÚ 2022-9-18 22:01:42 | ÏÔʾȫ²¿Â¥²ã
¹à¹à¹à¹àË®

8

Ö÷Ìâ

2Íò

»ØÌû

52

»ý·Ö

×¢²á»áÔ±

Rank: 2

»ý·Ö
52
·¢±íÓÚ 2022-11-13 11:50:03 | ÏÔʾȫ²¿Â¥²ã
dfdsafdsfdsfdsf

0

Ö÷Ìâ

1Íò

»ØÌû

0

»ý·Ö

Ö춻áÔ±

Rank: 3Rank: 3

»ý·Ö
0
·¢±íÓÚ 2022-12-7 01:27:13 | ÏÔʾȫ²¿Â¥²ã
лл¥Ö÷·ÖÏí

0

Ö÷Ìâ

1Íò

»ØÌû

0

»ý·Ö

Ö춻áÔ±

Rank: 3Rank: 3

»ý·Ö
0
·¢±íÓÚ 2022-12-7 10:55:04 | ÏÔʾȫ²¿Â¥²ã
ллÏÂÔØÀ´¿´¿´

29

Ö÷Ìâ

2Íò

»ØÌû

194

»ý·Ö

×¢²á»áÔ±

Rank: 2

»ý·Ö
194
·¢±íÓÚ 2023-3-9 20:18:20 | ÏÔʾȫ²¿Â¥²ã
Ó´Ó´Ó´Ó´Ó´ÒÔż

0

Ö÷Ìâ

2Íò

»ØÌû

0

»ý·Ö

Ö춻áÔ±

Rank: 3Rank: 3

»ý·Ö
0
·¢±íÓÚ 2023-4-26 09:22:37 | ÏÔʾȫ²¿Â¥²ã
hiŶ»Ø¸´iOS¾Í¿´¼û

0

Ö÷Ìâ

1Íò

»ØÌû

0

»ý·Ö

Ö춻áÔ±

Rank: 3Rank: 3

»ý·Ö
0
·¢±íÓÚ 2023-9-13 00:23:48 | ÏÔʾȫ²¿Â¥²ã
Ô´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´ÂëÔ´Âë

0

Ö÷Ìâ

1Íò

»ØÌû

0

»ý·Ö

Ö춻áÔ±

Rank: 3Rank: 3

»ý·Ö
0
·¢±íÓÚ 2023-10-29 22:24:14 | ÏÔʾȫ²¿Â¥²ã
¹ä¹ä¿´¿´ÇÆÇÆ

2

Ö÷Ìâ

2Íò

»ØÌû

99

»ý·Ö

×¢²á»áÔ±

Rank: 2

»ý·Ö
99
·¢±íÓÚ 2023-11-30 01:31:05 | ÏÔʾȫ²¿Â¥²ã
½è¿î½ð¶î¿´Á˾ÍÁ¢¿Ì
¸ß¼¶Ä£Ê½
B Color Image Link Quote Code Smilies

±¾°æ»ý·Ö¹æÔò

ÊÖ»ú°æ|СºÚÎÝ|ÍøÕ¾µØͼ|Ô´ÂëÂÛ̳ ( º£Íâ°æ )

GMT+8, 2024-11-24 11:44 , Processed in 0.074872 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí