使用动态发布有什么好处?
1. 避开 SAO 只能使用默认构造方法的限制。
2. 自主管理 SAO 的载入、卸载,以及其 URI。
RemotingServices通过使用类 RemotingServices 提供的方法,我们可以很轻松实现这些目标。
-
Marshal: 用于将 MarshalByRefObject 转换为 ObjRef 类的实例。
-
Connect:客户端可以用该方法创建远程代理对象的实例。
-
Disconnect:断开服务器远程对象与信道的连接。客户端代理在断开后调用任何方法都会触发 RemotingException。
-
Unmarshal:接受 ObjRef 并从它创建一个客户端代理对象。这个方法很少被使用,因为多数情况下我们并不会直接将 ObjRef 显示传递给客户端,而是交由 Remoting 基础结构来处理。
ObjRefObjRef 是远程对象的可序列化表示,用于跨应用程序域边界传输对象引用。为对象创建 ObjRef 称为封送。可以通过信道将 ObjRef 传输到另一个应用程序域(可能在另一个进程或计算机上)。达到其他应用程序域后,需立即分析 ObjRef,以便为该对象创建一个代理(通常连接到实际的对象)。此操作称为拆收处理 (Unmarshaling)。在拆收处理过程中,分析 ObjRef 以提取远程对象的方法信息,并创建透明代理和 RealProxy 对象。在透明代理注册到公共语言运行库之前,将已分析的 ObjRef 的内容添加到透明代理中。
ObjRef 包含:描述所封送对象的 Type 和类的信息,唯一标识特定对象实例的 URI,以及有关如何到达对象所在的远程处理分支的相关通信信息。
动态发布示例using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Services;
namespace Learn.Library.Remoting
{
/// <summary>
/// 远程类型
/// </summary>
public class Data : MarshalByRefObject
{
public void Test()
{
Console.WriteLine("Test AppDomain:{0}", AppDomain.CurrentDomain.FriendlyName);
}
public void Disconnect()
{
// 断开连接
RemotingServices.Disconnect(this);
}
}
public class RemotingTest2
{
/// <summary>
/// 服务器端代码
/// </summary>
static void Server()
{
AppDomain server = AppDomain.CreateDomain("server");
server.DoCallBack(delegate
{
TcpServerChannel channel = new TcpServerChannel(801);
ChannelServices.RegisterChannel(channel, false);
// 创建远程对象实例,当然也可以使用非默认构造方法。
// 此方式类似于 SAO.Singleton 。
Data data = new Data();
// 封送远程对象。
ObjRef objRef = RemotingServices.Marshal(data, "data");
});
}
/// <summary>
/// 客户端代码
/// </summary>
static void Client()
{
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
// 连接服务器,并创建代理实例。
Data data = (Data)RemotingServices.Connect(typeof(Data), "tcp://localhost:801/data");
// 调用远程对象方法。
data.Test();
// 调用远程对象方法,断开连接。
data.Disconnect();
// 再次调用远程对象方法时,因连接已断开,将抛出 RemotingException。
data.Test();
}
static void Main()
{
Server();
Client();
}
}
}
分享到:
相关推荐
Remoting事件(服务端广播改进).
Jenkins remoting 是一个可执行的 JAR,它在自动化服务器中实现通信层。 它用于主 <=> 代理和主 <=> CLI 通信。 通常,该库包含引导程序代码,用于将单独的 JVM 桥接到单个半共享空间中。 它包括:基于 TCP ...
Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触
学习 remoting 学习 remoting 学习 remoting 学习 remoting 学习 remoting 学习 remoting 学习 remoting 学习 remoting
C# .Net Remoting 两个简单示例,一个为普通的,一个为工厂模式的,更为安全一点.简单范例却是打好了一个框架,想要做东西,扩展就可以了. 使用说明和运行截图:http://www.our-code.com/news/2010105/n4920152.html 关于2...
remoting分布式技术入门教程,不可多得
Remoting双向通信v1.0
Microsoft .Net Remoting系列专题
remoting 广播
分布式 c# Remoting初学分布式 c# Remoting初学分布式 c# Remoting初学分布式 c# Remoting初学分布式 c# Remoting初学分布式 c# Remoting初学分布式 c# Remoting初学
本系列文章将简单介绍Remoting的一些基本概念,包括 应用程序域、Remoting构架、传值封送(Marshal by value)、传引用封送(Marshal by reference)、Remoting的基本操作、分离服务程序元信息和实现、在Windows Service...
remoting技术
remoting寄宿到IIS中,有些特别的优势,如: 1:可以借助于IIS的安全机制; 2:在硬件环境不允许的情况下,和IIS共用端口。 本程序,服务器采用IIS,客户端为一个console,演示了remoting如果宿主到IIS中。详细请看...
Microsoft .NET RemotingMicrosoft .NET Remoting Microsoft .NET RemotingMicrosoft .NET Remoting Microsoft .NET RemotingMicrosoft .NET Remoting
我感觉Remoting最重要的就是理解,远程对象的作用。我自己做的笔记,大家可以看一下: Remoting分布式系统开发 Remoting Object:分布式对象 Remoting能够开发P2P(qq) C/S 有点:可配置 安全,比webservice速度快 ...
Remoting Remoting 之SERVER Remoting 之SERVER
Remoting基本原理及其扩展机制 Remoting基本原理及其扩展机制 Remoting基本原理及其扩展机制 Remoting基本原理及其扩展机制
利用Remoting传送文件利用Remoting传送文件利用Remoting传送文件利用Remoting传送文件利用Remoting传送文件
Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触
Remoting技术,不同程序之间访问,修改其他程序数据。