王朝网络
分享
 
 
 

Remoting事件机制续

王朝other·作者佚名  2006-05-14
宽屏版  字体: |||超大  

(1)关闭一个客户端以后会影响其他的客户端事件

原因:客户端没有取消事件订阅就关闭了,触发事件的时候找不到事件订阅者

解决:遍历委托链,找到异常的对象,从委托链中卸下

(2)服务器端对客户端广播,客户端能收到其他客户端的事件处理信息

原因:使用了Singleton模式,共享远程对象

解决:因为需要远程对象有状态且不共享实例,所以只有客户端激活可以选择

修改后的服务端:

using System;

using System.Collections;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Runtime.Serialization.Formatters;

namespace RemoteServer

{

class MyServer

{

[STAThread]

static void Main(string[] args)

{

RemotingConfiguration.ApplicationName="RemoteObject.MyObject";

RemotingConfiguration.RegisterActivatedServiceType(typeof(RemoteObject.MyObject));

BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();

BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();

serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

IDictionary props = new Hashtable();

props["port"]=8888;

TcpChannel channel = new TcpChannel(props,clientProvider,serverProvider);

ChannelServices.RegisterChannel(channel);

Console.ReadLine();

}

}

}

修改后的远程对象:

using System;

namespace RemoteObject

{

[Serializable]

public class MyEventArgs:EventArgs

{

private int _rate;

private string _ip;

public int Rate

{

get

{

return _rate;

}

}

public string IP

{

get

{

return _ip;

}

}

public MyEventArgs(int rate,string ip)

{

this._rate=rate;

this._ip=ip;

}

}

public class MyObject:MarshalByRefObject

{

public delegate void MyEventHandler(object sender,MyEventArgs e);

public event MyEventHandler MyEvent;

public string tmp;

public int ALongTimeMethod(int a,int b,int time,string ip)

{

Console.WriteLine("来自"+ip+"的异步方法开始");

for(int i=1;i<=10;i++)

{

System.Threading.Thread.Sleep(time);

Console.WriteLine("来自"+ip+"的异步方法完成了"+i*10+"%");

OnMyEvent(new MyEventArgs(i,ip));

}

Console.WriteLine("来自"+ip+"的异步方法结束");

return a+b;

}

protected void OnMyEvent(MyEventArgs e)

{

if (MyEvent!=null)

{

foreach(Delegate d in MyEvent.GetInvocationList())

{

try

{

((MyEventHandler)d)(this,e);

}

catch

{

MyEvent-=(MyEventHandler)d;

}

}

}

}

}

public class EventClass:MarshalByRefObject

{

public void MyEvent(object sender,MyEventArgs e)

{

if(((MyObject)sender).tmp==e.IP)

Console.WriteLine("异步方法完成了"+e.Rate*10+"%");

}

}

}

修改后的客户端:

using System;

using System.Net;

using System.Collections;

using System.Text;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Tcp;

using System.Runtime.Serialization.Formatters;

class MyClient

{

private delegate int MyDelegate(int a,int b,int time,string ip);

private static MyDelegate md;

static RemoteObject.MyObject app;

static RemoteObject.EventClass ec;

static DateTime dt;

[STAThread]

static void Main(string[] args)

{

dt=DateTime.Now;

RemotingConfiguration.RegisterActivatedClientType(typeof(RemoteObject.MyObject),"tcp://localhost:8888/RemoteObject.MyObject");

BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();

BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();

serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

IDictionary props=new Hashtable();

props["port"]=0;

TcpChannel channel = new TcpChannel(props,clientProvider,serverProvider);

ChannelServices.RegisterChannel(channel);

app=new RemoteObject.MyObject();

ec=new RemoteObject.EventClass();

app.MyEvent+=new RemoteObject.MyObject.MyEventHandler(ec.MyEvent);

md=new MyDelegate(app.ALongTimeMethod);

AsyncCallback ac=new AsyncCallback(MyClient.CallBack);

IPHostEntry ipHE=Dns.GetHostByName(Dns.GetHostName());

Random rnd=new Random(System.Environment.TickCount);

string ip=ipHE.AddressList[0].ToString()+"("+rnd.Next(100000000).ToString()+")";

app.tmp=ip;

IAsyncResult Iar=md.BeginInvoke(1,2,500,ip,ac,null);

Method();

Console.WriteLine("用了"+((TimeSpan)(DateTime.Now-dt)).TotalSeconds+"秒");

ChannelServices.UnregisterChannel(channel);

Console.ReadLine();

}

public static void CallBack(IAsyncResult Iar)

{

if(Iar.IsCompleted)

{

Console.WriteLine("结果是"+md.EndInvoke(Iar));

app.MyEvent-=new RemoteObject.MyObject.MyEventHandler(ec.MyEvent);

}

}

public static void Method()

{

Console.WriteLine("主线程方法开始");

System.Threading.Thread.Sleep(5000);

Console.WriteLine("主线程方法结束");

}

}

之所以要在ip地址后面跟上随机数,是因为可能在一个机器上会打开多个客户端,需要在这个时候能在服务器端区分多个客户端。

备注:我的所有例子都是在客户端和服务器端部署远程对象的,其实这个做法不是很好,我们应该仅仅把接口部署在两地,远程对象仅仅部署在服务器端即可。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有