DotNet Remoting (一)
DotNet Remoting (一)
Remoting的功能
同Web Service的概念类似,Remoting可以使网络上的计算机之间共享类的数据和方法。通过Remoting,客户端可以调用服务端的类的方法,而不需要服务端运行IIS服务器。
Remoting的简单原理
1) 服务端有一个类
2)服务端为此类开通一个Remoting的服务
3)客户端想调用服务端此类的方法,首先建立一个连接,服务端和客户端通过此连接传递数据。通过此连接能够调用服务端的类的方法。(实际就是典型的C/S模式网络编程的应用,当然DotNet提供了高级类来支持Remoting。)
4) Remoting依赖于类的Serialization机制。可以这样理解Remoting过程:服务端序列化(次第存储)类的对象,经由网络连接序列化后的数据传递到客户端,客户端执行逆序列化过程,构造出和服务端相同的对象,然后就可以访问对象得数据和调用方法了。(DotNet提供BinaryFormatter和SoapFormatter两种方式序列化对象)
DotNet Remoting的组成元素
· A remoting class that provides C# methods used by client applications on the network.
· A remoting server, which hosts the remoting class, allowing network clients to connect and process method calls.
· A communication channel that passes method calls from the network client to the remoting server and returns the results back to the network client.
· A proxy class, which runs on the client machine. The proxy class accepts method calls from the client and passes them through the communication channel to the remote class.
· A client application, which passes method calls to the proxy class, and interprets information returned from the remoting class through the proxy class.
1)The Remoting Class(远程类)
The Remoting Class就是Remoting Server提供给客户端访问的类。有一个Application Domain(应用程序域)的概念,共享同一内存空间的应用属于同一个Application Domain。The Remoting Class就是不属于一个Application Domain的类。要访问不属于同一Application Domain的类的实例,就不能通过直接引用的方式,而要先序列化,然后通过连接通道再逆序列化。
2)The Remoting Server
The Remoting Server是一个应用程序,提供客户端访问The Remoting Class的服务。在此应用程序的Application Domain中注册了The Remoting Class,监听远程请求,然后做出响应。The Remoting Server有两种方式处理请求:一种是SingleCall object mode,一种是Singleton object mode。SingleCall方式当有请求时,就创建The Remoting Class的实例,当请求中断,就释放实例,实例间的信息是分开的。Singleton方式应用一个The Remoting Class的实例来响应所有请求,不同请求间可以共享信息,也一定程度提高效率。这两种方式各有用处。
3)The Communication Channel
服务端和客户端联系的通道。可以通过Tcp或HTTP两种方式来传递序列化信息。
4)The Proxy Class
The Proxy Class位于客户端,代理The Remoting Class的方法。客户端通过The Proxy Class访问The Remoting Class就如同访问本地的对象一样。The Remoting Class的Serialization和Deserialization都是The Proxy Class的责任。
5)The Client Program
The Client Program是访问The Remoting Class的程序。The Client Program访问The Remoting Class有两种方式。一种是自己建立The Communication Channel与The Remoting Server通信,访问The Remoting Class。一种是利用The Proxy Class。这两种方法各有优缺点(pros and cons),用The Proxy Class的方式可以使编程简单但是就绑定到一个Communication Channel上了。用另外一种方法编程相对复杂,但是如果The Remoting Server更改了,很容易相应地做出更改。
浙公网安备 33010602011771号