.net romoting 的EventWrapper類?
注意:防火墻的問題
---------------------------------------------------
//定義廣播服務接口
? ? [Serializable]
? ? public delegate void BroadCastEventHandler(string info);
? ? public interface IBroadCastService
? ? {
? ? ? ? event BroadCastEventHandler BroadCastEvent;
? ? ? ? void BroadCastingInfo(string info);
? ? }
? ? //事件封裝類
? ? [Serializable]
? ? public class EventWrapper : MarshalByRefObject,IRegisterSelf
? ? {
? ? ? ? public event BroadCastEventHandler LocalBroadCastEvent;
? ? ? ? //[OneWay]
? ? ? ? public void BroadCasting(string info)
? ? ? ? {
? ? ? ? ? ? if (LocalBroadCastEvent != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //LocalBroadCastEvent(message);
? ? ? ? ? ? ? ? BroadCastEventHandler tempEvent = null;
? ? ? ? ? ? ? ? int index = 1; //記錄事件訂閱者委托的索引,為方便標識,從1開始。
? ? ? ? ? ? ? ? foreach (Delegate del in LocalBroadCastEvent.GetInvocationList())
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? tempEvent = (BroadCastEventHandler)del;
? ? ? ? ? ? ? ? ? ? ? ? tempEvent(info);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch(Exception ee)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? errorlog.WriteLine(ee.ToString());
? ? ? ? ? ? ? ? ? ? ? ? errorlog.WriteLine("事件訂閱者" + index.ToString() + "發生錯誤,系統將取消事件訂閱!");
? ? ? ? ? ? ? ? ? ? ? ? LocalBroadCastEvent -= tempEvent;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? errorlog.WriteLine("EventWrapper中代理本地BroadCast事件為空!");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public override object InitializeLifetimeService()
? ? ? ? {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? #region IRegisterSelf 成員
? ? ? ? public void RegisterSelf()
? ? ? ? {
? ? ? ? ? ? RemotingConfiguration.RegisterWellKnownServiceType(typeof(EventWrapper), "EventWrapper", WellKnownObjectMode.Singleton);
? ? ? ? }
? ? ? ? #endregion
? ? }
? ? [Serializable]
? ? public class BroadcastEventArgs : EventArgs
? ? {
? ? ? ? private string msg = null;
? ? ? ? public BroadcastEventArgs(string message)
? ? ? ? {
? ? ? ? ? ? msg = message;
? ? ? ? }
? ? ? ? public string Message
? ? ? ? {
? ? ? ? ? ? get { return msg; }
? ? ? ? }
? ? }