首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网页制作-> CSS教程
Remoting事件机制续-.NET教程,评论及其它
作者:网友供稿 点击:28
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
(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地址后面跟上随机数,是因为可能在一个机器上会打开多个客户端,需要在这个时候能在服务器端区分多个客户端。



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


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·ASP.NET with C#使用md5,sha1加密初探-.NET教程,Asp.Net开发
·最简单的java分页算法-JSP教程,Java技巧及代码
·创建ASP.NET WEB自定义控件——例程1-.NET教程,Asp.Net开发
·探究客户端浏览器分辨率的自适应问题(1)-.NET教程,评论及其它
·基于.net开发平台项目案例集锦-.NET教程,Asp.Net开发
·编程初学者的良言警句-ASP教程,脚本编码
·Java中for循环中执行顺序问题及break, continue用法-JSP教程,Java技巧及代码
·Java数据库编程中的几个常用技巧-JSP教程,数据库相关
·ASP.NET2.0中的ClientScriptManager 类用法—如何添加客户端事件-.NET教程,评论及其它
·如何查找、删除表中重复的记录-数据库专栏,SQL Server
最新文章
·google adwords投放报告(二)_网赚技巧
·专访帝国软件的创造者:仍然在路上的80后_站长访谈
·王微:视频网站还要烧钱3年_站长访谈
·个人经验:做站的几大忌讳_站长心得
·大鸟的草根站长seo技巧(一)_站长心得
·flash与后台编码兼容性问题的解决方案_flash教程
·怎样才能把自己的网站做好?一定要知道的定律_站长心得
·论网站设计的十大要点2_站长心得
·网页设计配色应用实例剖析——橙色系2_站长心得
·被k的网站如何改进重新申请通过_google推广
相关主题
  • RemotingIIS承载方式-.NET教程,评论及其它
  • Remoting的承载方式是这样的-.NET教程,评论及其它
  • Remoting和Web服务的区别-.NET教程,Web Service开发
  • Remoting的事件机制(带具体例子)-.NET教程,评论及其它
  • Remoting服务端和客户端程序该这样模式来写-.NET教程,评论及其它
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭