C# .net remoting 学习笔记一

.net remoting 实现了跨进程对象间的访问(被访问的对象仅限于远程对象),可以作为进程间通信的一种方法。

现在将我理解的整理出来,有不对的地方请指正。文中所提的函数多有重载函数,我只是实验了其中的一种,

所以其它的函数是否也能使用,及如何使用。

文中例子远程对象类如下

 1 namespace Remotenamesapce
 2 {
 3     public class RObjectClass : MarshalByRefObject
 4     {
 5         public RObjectClass()
 6         {
 7 
 8         }
 9     }
10 }
View Code

 

1. 使用步骤:(1)定义信道 (2)注册信道 (3)注册远程对象类型。

2. 定义信道 

(1) ChannelServers 信道管理类。信道结构图如下:

其中继承IChannelSender 和 IChannelReceiver的类在客户端和服务端都可以使用。而TcpClientChannel只能在客户端使用。

TcpServerChannel只能在服务端使用。

 (2) 用户可以自定义信道。

3. 注册信道

     ChannelServers.RegisterChannel()将信道注册到.Net Remoting 运行库中。ChannelServiers还可以访问已注册的信道。

4. 注册远程对象类型

    服务端:

   (1)注册服务端激活对象

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RObjectClass),"strurl",WellKnownObjectMode.SingleCall);

    WellKnownObjectMode.SingleCall : 为每个客户端请求实例化一个远程对象。

    WellKnownObjectMode.Singleton : 每个客户端请求都由同一个对象提供服务。

    (2)客户端激活对象 当希望客户端的操作是有状态的时候,可以考虑使用这种对象。

            RemotingConfiguration.ApplicationName = "strurl"; // msdn上说是获取或设置远程处理应用程序的名称,我将其理解成

远程对象在信道中的名称。

     RemotingConfiguration.RegisterActivatedServiceType(typeof(RObjectClass));

     客户端:

      (1)获取服务器激活的远程对象

    方法一:使用Activator.GetObject; 例如:

          RObjectClass obj = (RObjectClass)Activator.GetObject(typeof(RObjectClass),"tcp://localhost:8085/strurl");

           方法二:使用RemotingServices.Connect, 例如:

          RObjectClass obj = (RObjectClass)RemotingServices.Connect(typeof(RObjectClass), "tcp://localhost:8085/strurl");

           方法三:使用使用new ,例如:

           RemotingConfiguration.RegisterWellKnownClientType(typeof(RObjectClass), "tcp://localhost:8085/strurl");

           RObjectClass obj = new RObjectClass();

      (2)激活客户端激活对象

           方法一:使用Activator.CreateInstance ,例如:

           object[] attrs = { new UrlAttribute("tcp://localhost:8085/strurl")};

           ObjectHandle handle = Activator.CreateInstance("Remotenamesapce", "Remotenamesapce.RObjectClass", attrs);

           RObjectClass obj = (RObjectClass)handle.Unwrap();

           // 其中Remotenamesapce为命名空间名字,也为程序集名称。

           方法二:使用new ,例如:

           RemotingConfiguration.RegisterActivatedClientType(typeof(RObjectClass), "tcp://localhost:8085/strurl");

           RObjectClass obj = new RObjectClass();

 5. 在客户端经过第4步获得远程对象(实际是一个透明代理对象)后客户端可以像使用本地对象一样使用远程对象了。

 

 

 

 

转载于:https://www.cnblogs.com/sunleinote/archive/2013/05/21/3091200.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常好用的C#.net的TCP控件,this.vmTcpIpServer1.Collapse = false; this.vmTcpIpServer1.EnableLog = false; this.vmTcpIpServer1.IdleTime = -1; this.vmTcpIpServer1.LocalUsingIpAddr = "127.0.0.1"; this.vmTcpIpServer1.Location = new System.Drawing.Point(9, 17); this.vmTcpIpServer1.LogFilePath = "D:\\AppLog"; this.vmTcpIpServer1.MaxLogShownLines = 30; this.vmTcpIpServer1.Name = "vmTcpIpServer1"; this.vmTcpIpServer1.PackageHeader = UNYC.TcpIp.PackageHeader.None; this.vmTcpIpServer1.PackageTailer = UNYC.TcpIp.PackageTailer.None; this.vmTcpIpServer1.PortNum = 30000; this.vmTcpIpServer1.SaveToLogFile = false; this.vmTcpIpServer1.ShowTransContents = false; this.vmTcpIpServer1.Size = new System.Drawing.Size(266, 405); this.vmTcpIpServer1.TabIndex = 0; // // vmTcpIpClient1 // this.vmTcpIpClient1.AutoRecover = false; this.vmTcpIpClient1.Collapse = false; this.vmTcpIpClient1.ConnRetries = -1; this.vmTcpIpClient1.EnableLog = false; this.vmTcpIpClient1.IdleTime = -1; this.vmTcpIpClient1.IpAddr = "192.168.100.231"; this.vmTcpIpClient1.Location = new System.Drawing.Point(311, 17); this.vmTcpIpClient1.LogFilePath = "D:\\AppLog"; this.vmTcpIpClient1.MaxLogShownLines = 100; this.vmTcpIpClient1.Name = "vmTcpIpClient1"; this.vmTcpIpClient1.PackageHeader = UNYC.TcpIp.PackageHeader.None; this.vmTcpIpClient1.PackageTailer = UNYC.TcpIp.PackageTailer.None; this.vmTcpIpClient1.PingInterval = 500; this.vmTcpIpClient1.PortNum = 912815; this.vmTcpIpClient1.SaveToLogFile = false; this.vmTcpIpClient1.ShowTransContents = false; this.vmTcpIpClient1.Size = new System.Drawing.Size(266, 405);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值