欢迎光临
我们一直在努力

Remoting编程知识二-.NET教程,.NET Framework

建站超值云服务器,限时71元/月

remoting高级知识

一、 如何使用iis作为激活代理

.net remoting和dcom之间的一个区别是前者不支持自动运行的服务器进程。需要人工启动服务器进程来注册用来远程激活的类和监听请求。而对于dcom,当远程客户端调用cocreateinstanceex或者其他激活api时,会自动运行服务器进程。

.net remoting 提供了两种方法来避免人工启动服务器。第一个是将服务器应用程序当做一个服务来实现。可以编写一个从

system.serviceprocess.service派生的服务,重载其中关键的需方法例如onstart和onstop。将服务器当做一个服务来实现的好处是你可以配置该服务以便系统启动时能自动运行该服务。

第二个方法是使用iis作为激活代理。iis本身就是一个服务,在大多数web servers运行时会一直启动。而且iis能够通过使用.net remoting机制来响应客户端激活对象的请求。使用iis有以下几个好处:

1、不再需要编写一个用来注册可远程化的类和监听端口的服务器,iis就是服务器。

2、可以使用iis鉴别远程调用者,也可以使用ssl来保护数据。

3、可以使用iis来管理端口。如果在一个机器上部署了两个传统的应用程序服务器,则需要你来保证这两个服务器使用不同的端口。使用iis作为宿主,则iis可以选择端口,这样可以简化发布和管理。

iis支持服务器端激活对象和客户端激活对象。可以使用程序注册(在global.asax中),也可以使用声明注册(在web.config中)。

1、服务器端激活对象

下面的web.config注册了使用iis激活的clock类:

<configuration>

<system.runtime.remoting>

<application>

<service>

<wellknown mode="singlecall" type="clock, clockserver" objecturi="clock.rem" />

</service>

</application>

</system.runtime.remoting>

</configuration>

注意clock的uri:clock.rem。使用iis注册的uri必须以.rem或者.soap结束,因为该扩展对应到iis原数据中的aspnet_isapi.dll和machine.config中的.net remoting 子系统。

使用iis激活对象都是通过http通道来与客户端进行通信。客户端必须注册http通道。下面是一个客户端如何创建一个clock实例,假设clock在本地机器上一个叫myclock的虚拟目录中。

httpclientchannel channel = new httpclientchannel ();

channelservices.registerchannel (channel);

remotingconfiguration.registerwellknownclienttype

(typeof (clock),"http://localhost/myclock/clock.rem");

clock clock = new clock ();

注意服务器和客户端都没有指定端口,iis选择该端口

2、客户端激活对象

web.config文件注册注册一个客户端激活对象clock

<configuration>

<system.runtime.remoting>

<application>

<service>

<activated type="clock, clockserver" />

</service>

</application>

</system.runtime.remoting>

</configuration>

下面是客户端的写法(依然假设clock在本地机器myclock虚拟目录中):

httpclientchannel channel = new httpclientchannel ();

channelservices.registerchannel (channel);

remotingconfiguration.registeractivatedclienttype

(typeof (clock), "http://localhost/myclock");

clock clock = new clock ();

注意:使用iis必须在虚拟目录中有一个可远程化的类,而且必须把 web.config放在单独的虚拟目录中(例如myclock),把dll放在bin子目录中(myclock\bin)。

二、 如何通过http通道传递二进制格式数据

使用iis的一个缺点是只能使用http通道。http通道将调用封装成soap消息,这样会增加消息的长度。iis只支持http通道,但它并不要求使用将通道调用封装成soap消息。默认情况下,http使用soap,因为它使用soapclientformattersinkprovide和

soapserverformattersinkprovider来作为序列化和反序列化消息的格式。可以使用binaryclientformattersinkprovider和

binaryserverformattersinkprovder来替换它们。二进制消息可以跟好的利用网络带宽。

下面的web.config文件注册了一个可以被iis激活的clock,它使用二进制替换了缺省的soap格式。

<configuration>

<system.runtime.remoting>

<application>

<service>

<wellknown mode="singlecall" type="clock, clockserver"

objecturi="clock.rem" />

</service>

<channels>

<channel ref="http server">

<serverproviders>

<formatter ref="binary" />

</serverproviders>

</channel>

</channels>

</application>

</system.runtime.remoting>

</configuration>

客户端写法如下:

httpclientchannel channel = new httpclientchannel

("httpbinary", new binaryclientformattersinkprovider ());

channelservices.registerchannel (channel);

remotingconfiguration.registerwellknownclienttype

(typeof (clock), "http://localhost/myclock/clock.rem");

clock clock = new clock ();

当使用配置文件时,写法为:

remotingconfiguration.configure ("client.exe.config");

clock clock = new clock ();

配置文件内容如下:

<configuration>

<system.runtime.remoting>

<application>

<client>

<wellknown type="clock, clockserver"

url="http://localhost/myclock/clock.rem" />

</client>

<channels>

<channel ref="http client">

<clientproviders>

<formatter ref="binary" />

</clientproviders>

</channel>

</channels>

</application>

</system.runtime.remoting>

</configuration>

使用相同的方法,你也可以在tcp通道中使用soap格式封装消息。你甚至可以将自己的格式化方法插入到现有的通道中。

三、 如何使用事件和代理

假设你创建了一个clock 类,包括一个newhour事件,代码如下:

public delegate void newhourhandler (int hour);

public class clock : marshalbyrefobject

{

public event newhourhandler newhour;



}

在iis上使用的web.config文件如下:

<configuration>

<system.runtime.remoting>

<application>

<service>

<wellknown mode="singleton" type="clock, clockserver"

objecturi="clock.rem" />

</service>

<channels>

<channel ref="http" />

</channels>

</application>

</system.runtime.remoting>

</configuration>

注意ref属性,http值使得two-way httpcannel代替了one-way httpserverchannel。

客户端写法如下:

remotingconfiguration.configure ("client.exe.config");

clock clock = new clock ();

clock.newhour += new newhourhandler (onnewhour);

.

.

.

public void onnewhour (int hour)

{

// newhour event received

}

<configuration>

<system.runtime.remoting>

<application>

<client>

<wellknown type="clock, clockserver"

url="http://localhost/myclock/clock.rem" />

</client>

<channels>

<channel ref="http" port="0" />

</channels>

</application>

</system.runtime.remoting>

</configuration>

客户端也注册了一个two-way httpchannel,并且指定端口号为0。0值使得通道监听回调,当然也可以允许.net框架来选择端口数。

四、 如何异步调用一个可远程化对象

缺省情况下,,调用远程对象是同步的。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Remoting编程知识二-.NET教程,.NET Framework
分享到: 更多 (0)

相关推荐

  • 暂无文章