.NET Remoting简单介绍

微软的.NET Remoting提供了一种允许对象通过应用程序域与另一对象进行交互的框架,从结构上而言,.NET Remoting对象非常适合通过 网络访问资源,而又无需处理由基于SOAP的WebServices所带来的难题。下面介绍创建一个简单的.NET Remoting分布式远程方法调用 程序一般所需要的几个步骤。

第一步:创建共享远程对象

创建一个C# Library,并将其命名为RemoteObject。这将创建一个我们的.NET Remote客户端和服务器端用来通讯的“共享命令集”。
 public class RemoteObject : System.MarshalByRefObject
 {
     public RemoteObject()
     {
        System.Console.WriteLine("New Referance Added!");
     }

     public int sum(int a, int b)
     {
        return a + b;
     }
 }
名字空间是对象所需要的。请记住,如果得到System.Runtime.Remoting.Channels.Tcp名字空间 不存在的信息,请检查是否象上面的代码那样添加了对System.Runtime.Remoting.dll的引用。
 
using System;
using System.Runtime;

我们为对象使用的名字空间是RemoteSample,下面的对象是 MarshalByRefObject,在其中我们创建了一个引用和包括服务器端操作全部完成所需要的所有工作。

namespace RemoteSample
{
 public class RemoteObject : System.MarshalByRefObject
 {
    public RemoteObject()
    {
       System.Console.WriteLine("New Referance Added!");
    }

    public int sum(int a, int b)
    {
       return a + b;
    }
 }
}//RemoteSample 名字空间结束

保存文件,命名为RemoteObject.cs
用命令行csc /t:library RemoteObject.cs 编译文件,就会得到一个RemoteObject.DLL文件,并可以在编译其他的C#文件中使用它。


第二步:创建 服务器对象

创建服务器对象,并将它命名为RemoteServer。在创建服务器对象时必须实现以下操作:

1)构 造服务器信道。
TcpServerChannel是.NET remoting支持的二种信道类型中的一种,可以将它设置为对象对来自哪一个端口的请求进行回 应,ChannelServices.RegisterChannel将把该端口号与操作系统中的TCP/IP栈绑定。

TcpServerChannel channel = new TcpServerChannel(8808);
ChannelServices.RegisterChannel(channel);

也可以设置为另一种的信道类型HTTP, 只要简单地用System.Runtime.Remoting.Channels.Http名字空间中的 HttpServerChannel对象替换即可。使用HTTP和TCP信道之间的区别在于:如果应用程序是在局域网上运行,则最好使用TCP信道,因为 它的性能要好于HTTP信道;如果应用程序是在互联网上运行,则有时候根据防火墙的配置,HTTP是唯一的选择。需要记住的是,如果使用了防火墙软件,则 防火墙应该配置成允许TCP数据流量通过你为对象选择的端口。

2)将服务端上的对象 Type 注册为已知类型。
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
"RemoteObject", WellKnownObjectMode.SingleCall);
这 行代码设置了服务中的一些参 数和把欲使用的对象名字与远程对象进行绑定,第一个参数是绑定的对象,第二个参数是TCP或HTTP信道中远程对象名字的字符串,第三个参数让容器知道, 当有对对象的请求传来时,应该如何处理对象。尽管WellKnownObjectMode.SingleCall对所有的调用者使用一个对象的实例,但它 为每个客户生成这个对象的一个实例。如果用WellKnownObjectMode.SingleCall则每个传入的消息由同一个对象实例提供服务。

完整的对象代码如下所示:
 using System; using System.Runtime;
 using System.Runtime.Remoting;
 using System.Runtime.Remoting.Channels;
 using System.Runtime.Remoting.Channels.Tcp;
 using RemoteSample;
 namespace RemoteSampleServer
 {
  public class RemoteServer
  {
      public static void Main(String[] args)
      {
         TcpServerChannel channel = new TcpServerChannel(8808);
         ChannelServices.RegisterChannel(channel);
         RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
                                  "RemoteObject", WellKnownObjectMode.SingleCall);
         System.Console.WriteLine("Press Any Key");
         System.Console.ReadLine();
       }
   }
 }
保存文件,命名为 RemoteServer.cs
用命令行csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteServer.cs 编译这一程序生成的RemoteServer.EXE文件。


第三步:创建 Remote客户端程序
创 建服务器对象,并将它命名为RemoteClient。在创建服务器对象时首先创建了一个TCP客户端信道,该信道并不 是绑定在一个端口上的;其次获取一个对远程的RemoteObject对象的引用。 Activator.GetObject方法返回一个对象类型的值,我们随后会将它返回的值赋予RemoteObject。我们传给它的参数与在服务器对 象中传递给RemotingConfiguration的参数非常地相似,第一个参数是对象类型的,第二个参数是远程对象的URI。

 ChannelServices.RegisterChannel(new TcpClientChannel());
 RemoteObject remoteobj = (RemoteObject)Activator.GetObject( typeof(RemoteObject),
                       "tcp://localhost:8808/RemoteObject");

 RemoteClient 的全部代码如下所示:

 using System;
 using System.Runtime.Remoting; /> using System.Runti
me.Remoting.Channels;
 using System.Runtime.Remoting.Channels.Tcp;
 using RemoteSample;
 namespace RemoteSampleClient
 {
  public class RemoteClient
  {
     public static void Main(string[] args)
     {
        ChannelServices.RegisterChannel(new TcpClientChannel());
        RemoteObject remoteobj = (RemoteObject)Activator.GetObject( typeof(RemoteObject),
                           "tcp://localhost:8808/RemoteObject");
        Console.WriteLine("1 + 2 = " + remoteobj.sum(1,2).ToString());
        Console.ReadLine();//在能够看到结果前不让窗口关闭
     }

   }
 }

保存文件,命名为RemoteClient.cs
用命令行csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteClient.cs 编译这一程序生成的RemoteClient.EXE文件。


第四步:测试
在windows中运行Server.exe,然后打开另一个窗体运行Client.exe。
如果一切正常的话,客户端输出: 1 + 2 = 3。

posted @ 2014-03-15 11:40  Net-Spider  阅读(293)  评论(0)    收藏  举报