.NET Remoting 基础学习教程(一)

.NET Remoting 基础学习教程(一)

Raymond Tang Raymond Tang 0 1102 0.19 index 5/9/2009

.NET Remoting 可以用于处理激活、分布式验证、生存期和调用环境等方面的工作。它与Xml Web Service不同,在Xml Web Service中对象是抽象的,客户部需要知道服务器对象的类型,且与.net Remoting 不同,它是独立于平台的。.net Remoting 可以用于访问另一个应用程序域中的对象,无论两者处于同一进程还是不同的进程或者不同的系统中,都可以进行相互间的访问。

示意图如下:

下面以一个简单的例子进行介绍,这个例子是Wrox出版的《C#高级编程第四版》中.net Remoting章节的例子。

先建一个类库项目,取名为DoNetRemoting

其中有个类Hello,代码如下:

using System;
using System.Collections.Generic;
using System.Text;namespace DotNetRemoting
{
    public class Hello : MarshalByRefObject
    {
       public Hello()
       {
          Console.WriteLine("Constructor called.");
       }       ~Hello()
       {
          Console.WriteLine("Destructor called.");
       }       public string Greeting(string name)
       {
          Console.WriteLine("Greeting called");
          return "Hello," + name;
       }
    }
}

这只是一个很简单的类,用于测试。

再新建一个.net Remoting服务器上要用到的控制台项目RemotingServer,且引用DotNetRemoting项目以及System.Runtime.Remoting组件

其中的Program类如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using DotNetRemoting;namespace RemotingServer
{
    class Program
    {
       static void Main(string[] args)
       {
          TcpServerChannel channel = new TcpServerChannel(8086);
          ChannelServices.RegisterChannel(channel, false);
          RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
          Console.WriteLine("press any key to exit.");
          Console.ReadLine();       }
    }
}

这个类中的TcpServerChannel channel = new TcpServerChannel(8086);语句表示要创建服务器TCP端口8086的信道,用于接收客户端的请求等(自己对于通信,不是很了解,如果出现了一些描述性或者通信方面的知识错误,恳请大家指正),这个端口可以自己更改,但要注意防火墙是否屏蔽了此端口,否则客户端无法连接的。

再新建个项目也是控制台应用程序RemotingClient,也要引用DotNetRemoting项目以及System.Runtime.Remoting组件

其中的Program类的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using DotNetRemoting;namespace RemotingClient
{
    class Program
    {
       static void Main(string[] args)
       {
          ChannelServices.RegisterChannel(new TcpClientChannel(), false);
          Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");
          if (obj == null)
          {
             Console.WriteLine("cannot locate server.");
             return;
          }
          else
          {
             for (int i = 0; i < 5; i++)
             {
                Console.WriteLine(obj.Greeting("Raymond"));
             }
          }
          Console.ReadLine();
       }
    }
}

其中的语句Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");则是创建对服务器端对象的访问代理,这里的tcp://localhost:8086/Hi表示我在本地测试的,8086是服务器端的端口,而Hi则是RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);中的名称标识Hi

最终解决方案布局如下:

设置解决方案的属性为多启动项目,同时启动RemotingClient和RemotingServer 项目,按F5启动调试则可以看到下面的结果:

这个是服务器端的运行结果

下面的是客服端的运行结果

如果你的有防火墙则需要开启tcp的8086端口

下面我将客户端部署到我电脑上的虚拟机上Windows Server 2003,同时把tcp://localhost:8086/Hi中的Localhost更改为我主机的电脑。

把主机上的服务器端的RemotingServer控制台应用程序开启

再点击虚拟机里边的RemotingClient.exe就可以看到下面的运行结果

而此时主机上的服务器端结果如下(虚拟机上我连续运行了两次):

.net

Join the Discussion

View or add your thoughts below

Comments