Remoting学习
来源:互联网 发布:进淘宝天下是签阿里吗 编辑:程序博客网 时间:2024/05/29 10:35
一、几个关于Remoting的概念
1.远程对象:
远程对象类是从MarshalByRefObject类中派生的。跨越应用程序域调用这个类需要使用代理。.NET Remoting支持两种类型的远程对象:知名的(Well-known)远程对象和客户激活(Client-activated)远程对象。远程对象其实包括两层含义:
操作远程对象:对象运行在远程,客户段向他发送消息;
传递远程对象:将远程对象拿到本地,或者将本地对象发送过去,对副本进行操作。
2.激活:
使用new运算符可以激活远程对象。还有其它一些方式也可以激活远程对象,在以后的随笔里面我会介绍。
3.通道:
一个远程对象使用通道发送和接收消息。服务器选择一个通道来监听请求,客户端选择通道来和服务器通讯。Remoting提供了内置的通道:TCP通道和HTTP通道,我们也可以编写自己的通道。
4.编组:
数组通过应用程序域被传递的过程称为编组。将变量作为远程对象的参数来发送时,这个变量必须被转换,以便能够通过应用程序域发送该变量。
5.监听:
使用监听,能够将某些功能置入到方法调用链中。如果调用某个对象的方法,监听层便能够捕获调用来转换方法调用,或是完成某些日志记录。.NET Remoting调用链的每一部分都是用监听。
二、Remoting技术的实现
在VS.NET2005的解决方案中创建三个Project(1.远程对象RemoteObject;2.服务器端RemoteServer;3.客户端RemoteClient)
1.创建远程对象RemoteObject
需要继承继承System.MarshalByRefObject
代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace RemoteObject
{
publicclassMyObject:MarshalByRefObject
{
public MyObject()
{
Console.WriteLine("New Reference added!");
}
publicint Add(int firstNumeber, int lastNumber)
{
return firstNumeber + lastNumber;
}
}
}
2.创建宿主应用程序:需要添加RemoteObject的引用,并且using这个命名空间
注册通道
注册服务器激活的远程对象
运行宿主程序
代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteObject;
namespace RemoteServer
{
classProgram
{
staticvoid Main(string[] args)
{
//发布时只需要将exe文件及RemoteObject的dll文件复制到远程,本地可以直接调用
TcpServerChannel channel = newTcpServerChannel(8808);
ChannelServices.RegisterChannel(channel,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyObject), "RemoteObject", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Press Any Key...");
System.Console.ReadLine();
}
}
}
配置文件App.Config的代码如下:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<applicationname="RemoteServer">
<service>
<wellknowntype="RemoteObject.MyObject,RemoteObject"object=""Uri="RemoteObject.MyObject"mode="Singleton"/>
</service>
<channels>
<channelref="tcp"port="9999"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
3.3.建立客户端程序:需要添加远程对象与服务器端的引用,并且using其命名空间
注册通道
根据URL得到对象代理
使用代理调用远程对象
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using RemoteServer;
using RemoteObject;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemoteClient
{
classProgram
{
staticvoid Main(string[] args)
{
try
{
ChannelServices.RegisterChannel(newTcpClientChannel());
MyObject remoteobj = (MyObject)Activator.GetObject(
typeof(MyObject), System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
//从配置文件中获取URL信息,需要修改客户端文件名相同的那个config文件
//本地文件需要exe文件及对应的config文件
Console.WriteLine("1 + 2 = " + remoteobj.Add(1,2).ToString());
Console.ReadLine();//在能够看到结果前不让窗口关闭
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
配置文件App.Config代码如下:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
<appSettings>
<addkey="ServiceURL"value="tcp://localhost:8808/RemoteObject"/>
</appSettings>
</configuration>
- Remoting学习
- Remoting学习
- 开始学习.net remoting
- 学习.NET remoting 啦!
- .Net Remoting学习笔记
- Remoting学习一
- Remoting 学习笔记
- Remoting学习笔记
- .Net Remoting学习
- C#学习笔记:.NET Remoting
- Remoting学习之配置篇
- 今日阅读-20081231-学习remoting
- remoting
- Remoting
- Remoting
- remoting
- remoting
- Remoting
- 微软的正则表达式教程(五)
- Google孤独的IPO之旅
- 中国软件产业的出路在哪里?
- Oracle存储过程中去掉重复字符串的函数
- ajax资料收集-Ajax文章(教程和实例)---Ajax资源下载(源码和教程)---Ajax规范标准
- Remoting学习
- 拒绝水货 N70 香港正规行货鉴别
- 关于邮件中Cannot convert MIME message to Notes Rich Text message错误的问题的解决
- 今天服务器有被入侵的迹象
- RSS的作用及存在的意义
- 生命是一连串长期而持续的累积
- 价廉物美的三维克莱因瓶(Klein Bottle)
- 如果让我重做一次研究生
- 互联网上的UFO教派