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

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 认证考试-> ORACLE认证
Remoting编程知识一-.NET教程,.NET Framework
作者:网友供稿 点击:545
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
remoting基础


基本原理
当客户端创建远程remotableclass的一个实例,.net框架在客户端应用程序域中产生一个代理。该代理看起来就像实际对象。代理收到调用后,通过通道连接到远程的对象。



一、编写步骤

第一步


编写一个dll,其中包含所要remottable的类

public class remotableclass:marshalbyrefobject

{

….

}




第二步

服务器进程注册该remotable 类以便其他应用程序可以激活。根据该对象是如何激活,服务器通过两种静态方法来注册:registeractivatedservicetype或者registerwellknownservicetype。下面的语句使用registerwellknownservicetype来注册remotableclass,以便远程激活。

remotingconfiguration.registerwellknownservicetype(

typeof(remotableclass), //remotable类

“remoteobject”, // remotable类的uri

wellknownobjectmode.singlecall); //激活模式

第一个参数是指能远程化的类。

第二个是指客户端使用来激活对象的uri----也就是客户端告诉服务器来激活

remotableclass实例的uri。

第三个参数指定激活模式。有两种选择。wellknownobjectmode.singlecall是指为客户端的每一次调用创建一个新的实例。wellknownobjectmode.singleton是指创建一个remotableclass实例来处理所有客户端的调用。

第三步

为了使客户端可以使用remotableclass,服务器进程必须创建,注册一个通道。该通道提供对象和远程客户端交流的一个渠道。在服务器端,.net框架提供了两种通道:

system.runtime.remoting.channels.tcp.tcpserverchannel:可以接受远程客户端的tcp连接。

system.runtime.remoting.channels.http.httpserverchannel:接受http连接。

下面的语句创建一个在1234端口监听的tcpserverchannel通道,并用.net框架注册:

tcpserverchannel channel = new tcpserverchannel(1234);

channelservices.registerchannel(channel);

下面的语句注册了一个在1234端口监听的http通道:

httpservicechannel channel = new httpserverchannel(1234);

channelservices.registerchannel(channel);

tcpserverchannel更有效率一点。httpserverchannel是使用iis作为远程激活代理时使用的选择。


第四步

在客户端要想创建远程类的一个实例,也必须做一些注册。

第一必须注册一个客户端通道。.net框架提供了两种类型的客户端通道:tcpclientchannel和httpclientchannel,分别和服务器端通道相对应。

第二,如果客户端想使用new操作符来生产远程对象,必须将远程对象注册到本地应用程序域。

remotingconfiguration.registerwellknownclienttype是在客户端注册一个类。

remotingconfiguration.registerwellknownservicetype是在服务器上注册一个类。

下面的代码在客户端注册了一个tcp通道,而且也将remotableclass注册到本地应用程序域中:

tcpclientchannel channel = new tcpclientchannel();

channelservices.registerchannel(channel);



remotingconfiguration.registerwellknownclienttype(

typeof(remotableclass),

“tcp://localhost:1234/remoteobject”);

第二个参数是指远程对象的url。

协议必须匹配应用程序注册的通道协议。

可以使用机器名或者ip地址来替换localhost。

端口数必须好服务器端监听的端口数一样。

对象uri,必须和服务器用registerwellknownservicetype注册的匹配。

第五步

在客户端使用new来产生代理:

remotableclass rc = new remotableclass();

这个操作在客户端应用程序域中产生一个代理,返回remotableclass的一个引用。

二、实际范例

clockserver.cs

using system;
public class clock : marshalbyrefobject
{
public string getcurrenttime ()
{
return datetime.now.tolongtimestring ();
}
}
timeserver.cs

using system;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
class myapp
{
static void main ()
{
tcpserverchannel channel = new tcpserverchannel (1234);
channelservices.registerchannel (channel);

remotingconfiguration.registerwellknownservicetype
(typeof (clock), "clock", wellknownobjectmode.singlecall);

console.writeline ("press enter to terminate...");
console.readline ();
}
}
timeclient.cs

using system;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
class myapp
{
static void main ()
{
tcpclientchannel channel = new tcpclientchannel ();
channelservices.registerchannel (channel);

remotingconfiguration.registerwellknownclienttype
(typeof (clock), "tcp://localhost:1234/clock");
clock clock = new clock ();
console.writeline (clock.getcurrenttime ());
}
}
编译:

1. csc /t:library clockserver.cs
2. csc /r:clockserver.dll timeserver.cs
3. csc /r:clockserver.dll timeclient.cs
要将clockserver.dll拷贝到客户端。因为创建远程对象的代理时,.net框架需要描述clock类的原数据。它可以从dll中得到原数据。



三、配置方式

timeserver和timeclient在其源代码内部注册通道和远程化的类。这样有个缺点,一旦任何一个注册数据改变,你必须要修改源代码,并重新编译。

这就是为什么.net框架支持另一种形式的注册。声明注册是通过调用静态

remotingconfiguration.configure方法来从config文件中得到信息。

范例如下:

clockserver.cs

using system;

public class clock : marshalbyrefobject
{
public string getcurrenttime ()
{
return datetime.now.tolongtimestring ();
}
}

timeserver.cs

using system;
using system.runtime.remoting;

class myapp
{
static void main ()
{
remotingconfiguration.configure ("timeserver.exe.config");
console.writeline ("press enter to terminate...");
console.readline ();
}
}
timeserver.exe.config

<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="singlecall" type="clock, clockserver"
objecturi="clock" />
</service>
<channels>
<channel ref="tcp server" port="1234" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
timeclient.cs

using system;
using system.runtime.remoting;

class myapp
{
static void main ()
{
remotingconfiguration.configure ("timeclient.exe.config");
clock clock = new clock ();
console.writeline (clock.getcurrenttime ());
}
}
timeclient.exe.config

<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type="clock, clockserver"
url="tcp://localhost:1234/clock" />
</client>
<channels>
<channel ref="tcp client" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
该方式的缺点是配置文件可以被修改和删除。


四、激活方式

.net框架将可远程化对象分为两种:服务器激活对象和客户端激活对象。服务器端激活对象是通过remotingconfiguration’sregisterwellknownservicetype和

registerwellknownclienttype方法注册的。上面的范例都是服务器端激活对象。客户端激活对象是通过registeractivateservicetype和registeractivatedclienttype注册的。



服务器端激活对象被称为服务器激活的,因为当客户端使用new,只有一个代理被创建。实际对象知道通过代理来调用一个方法时才被创建(激活)。换句话说,不是客户端决定什么时候去创建物理上的真正对象。客户端激活对象在客户端使用new时就在服务器上创建。这个是第一个差别。

第二个差别是客户端激活对象可以使用非缺省构造函数(带参数的构造函数)激活。服务器端机会对象不支持非缺省构造函数,因为使用new只是创建一个代理,并没有创建对应的实际对象。客户端激活对象可以通过new同时创建代理和对象。

第三个差别是客户端和对象是如何联系在一起的。当注册服务器激活对象时,你可以指定激活模式来决定为每一个请求创建一个对象实例还是创建一个对象实例来服务所有的请求。这两中激活模式是:

wellknownobjectmode.singlecall:为每个请求创建一个唯一的对象实例。

wellkonwnobjectmode.singleton:创建一个对象实例来服务所有的请求

通常根据环境来选择合适的激活模式。举例来说,如果一个远程化对象提供了一个”one-shot”服务,不需要在多次调用间保持状态或者不需要在所有客户端同享状态,那么singlecall是个正确的选择。因为每一次的请求产生的是一个新的对象实例。如果想在客户端之间传递数据,则要使用singleton。

singleton对象一个值得注意的地方是线程的同步问题。当两个客户端同时调用该对象的方法时,可能会出现错误,这时要使用.net框架提供的同步机制。



客户端激活对象提供第三种选择。当使用客户端激活对象时,该对象仅为此客户端服务,可以在多次调用间保持状态。



single-call服务器激活对象,singleton服务器激活对象和客户端激活对象的提供了三种不同的激活模式。当不需要在所有客户端共享状态时,则使用single-call。当要在所有客户端共享状态时则使用singleton。当不需要所有的客户端连接到同一个对象,只要保持该客户端自己的状态时,则使用客户端激活对象。



程序范例:

stopwatch.cs

using system;
public class stopwatch : marshalbyrefobject
{
datetime mark = datetime.now;
public void start ()
{
mark = datetime.now;
}
public int stop ()
{
return (int) ((datetime.now - mark).totalmilliseconds);
}
}
stopwatchserver.cs

using system;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
class myapp
{
static void main ()
{
tcpserverchannel channel = new tcpserverchannel (1234);
channelservices.registerchannel (channel);

remotingconfiguration.registeractivatedservicetype
(typeof (stopwatch));

console.writeline ("press enter to terminate...");
console.readline ();
}
}
stopwatchclient.cs

using system;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.tcp;
class myapp
{
static void main ()
{
tcpclientchannel channel = new tcpclientchannel ();
channelservices.registerchannel (channel);
remotingconfiguration.registeractivatedclienttype
(typeof (stopwatch), "tcp://localhost:1234");
stopwatch sw = new stopwatch ();
sw.start ();
console.writeline ("press enter to show elapsed time...");
console.readline ();
console.writeline (sw.stop () + " millseconds");
}
}
五、activator.getobject和activator.createinstance方法

new操作符并不是激活远程对象的唯一方法。.net框架提供了其他的激活方法:getobject和createinstance。它们都是system.activator类的成员。getobject被用来激活在服务器端激活的对象,而createinstance被用来激活在客户端激活的对象。

当使用getobject或者createinstance来激活远程对象时,不再需要调用registeractivatedclienttype或者registerwellknownclienttype来注册服务器上可远程化的类。例如:激活在服务器端激活的对象时:

remotingconfiguration.registerwellknownclienttype(typeof(clock),”tcp://localhost:1234/clock”);

clock clock = new clock();

可以使用下面的方法代

clock clock =(clock) activator.getobject(typeof(clock,”tcp://localhost:1234/clock”);

激活客户端对象时:

remotingconfiguration.registeractivatedclienttype(typeof(stopwatch),”tcp://localhost:1234”);

stopwatch sw = new stopwatch();

可以这样的方式:

object[] url ={new urlattribute(“tcp://localhost:1234”)};

stopwatch sw =(stopwatch) activator.createinstance(typeof(stopwatch),null,url);

为什么要使用它们来代替new呢?因为在你仅知道url和接口时,getobject和createinstance可以仍使用。假设改变clock类,它实现一个iclock接口。

使用getobject时:

iclock ic = (iclock)activator.getobject(typeof(iclock),”tcp://localhost:1234/clock”);

如果使用new,则会出现编译错误,因为new不能接受一个接口名称:

remotingconfiguration.registerwellknownclienttype
(typeof (iclock), "tcp://localhost:1234/clock");
iclock ic = new iclock ();



六、对象生存期和租用期

一个single-call服务器端激活对象只在方法调用期间生存。之后,被垃圾回收器标记为删除。singleton 服务器激活对象和客户端激活对象不一样,他们的生存期被租用控制。租用是一个对象,它实现了定义在system.runtime.remoting.lifetime名称空间的ilease接口。

singleton 服务器端激活对象和客户端激活对象缺省的租用对象有一个5分钟的initialleasetime,2分钟的renewoncalltime,5分钟的currentleasetime。如果对象没有方法被调用,当currentleasetime为0时它被清除,也就是5分钟后被清除。




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

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·一个特牛的日期时间判断正则表达式-ASP教程,正则表达式
·Remoting编程知识一-.NET教程,.NET Framework
·从客户端检测到有潜在危险的Request.Form 值-ASP教程,客户端相关
·关于DataRow和DataColumn的一点个人简单理解-.NET教程,数据库应用
·DataTable.Select使用小心得-.NET教程,数据库应用
·在Java中如何模拟多继承-JSP教程,Java技巧及代码
·上海移动数据网综合网管的实现
·在VC中用GDI+绘制角度可变的颜色渐变效果-.NET教程,VB.Net语言
·超漂亮的绿色按扭制做-网页设计,Photoshop
·Java获取Html变量的值-JSP教程,Java技巧及代码
最新文章
·阿里联盟:关于结算时间问题的统一说明_网赚技巧
·谷歌官方组织adsense优化大赛_网赚技巧
·google adsense 2007巡讲大会上海站总结_网赚技巧
·网络只是一个开始:专访91now站长小鱼头_站长访谈
·asp.net应用程序资源访问安全模型_asp.net技巧
·给那些迷惑于做垃圾站的站长们_站长心得
·免费——不是威客网站的杀手锏_站长心得
·圈圈浅谈个人网站发展和赚钱的模式-网站推广的口碑篇_站长心得
·说网解络之web2.0概念诠释(1)_站长心得
·google adsense系列技巧100条_google推广
相关主题
  • RemotingIIS承载方式-.NET教程,评论及其它
  • Remoting的承载方式是这样的-.NET教程,评论及其它
  • Remoting和Web服务的区别-.NET教程,Web Service开发
  • Remoting的事件机制(带具体例子)-.NET教程,评论及其它
  • Remoting服务端和客户端程序该这样模式来写-.NET教程,评论及其它
  • 西部数码虚拟主机

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