一、实用类:
1、System.MarshalByRefObject :
      系统中远程调用的对象必须是从MarshalByRefObject对象中派生出来的;
2、System.Runtime.Remoting.Channels.Tcp.TcpServerChannel :
      服务器端的Tcp信道;
3、System.Runtime.Remoting.Channels.Http.HttpServerChannel :
      服务器端的Http信道;
4、System.Runtime.Remoting.Channels.ChannelServices :
      注册信道,使之可用于远程对象;
5、System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnowServiceType :
      指定远程对象中类的类的类型,客户机使用的URI和模式;
6、System.Runtime.Remoting.Channels.Tcp.TcpClientChannel :
      客户端的Tcp信道;
7、System.Runtime.Remoting.Channels.Http.HttpClientChannel :
      客户端的Http信道。

二、简单的示例

1、创建远程对象,在这里创建一个dll程序集,这个dll在服务器和客户机代码中都会用到。
      创建名为RemoteHello.dll的程序集

 

using System;
using System.Collections.Generic;
using System.Text;

namespace RemoteHello
{
     public class Hello:System.MarshalByRefObject
     {
         public Hello()
         {
             Console.WriteLine("Constructor called");
         }

         ~Hello()
         {
             Console.WriteLine("Destructor called");
         }

         public string HelloWorld(string name)
         {
             Console.WriteLine("Hello World!");
             return "Hi," + name;
         }
    
     }
}

 

 

2、创建服务器。需要引用System.Runtime.Remoting程序集和之前创建的RemoteHello.dll程序集。在此创建名为HelloServer的Console Application。
  
名字空间是对象所需要的。请记住,如果得到System.Runtime.Remoting.Channels.Tcp名字空间不存在的信息,请检查是否添加了对System.Runtime.Remoting.dll的引用。

 

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 RemoteHello;
namespace HelloService
{
     class HelloServer
     {
         static void Main(string[] args)
         {
             TcpServerChannel channel = new TcpServerChannel(6666);
             //ChannelServices.RegisterChannel(channel);
             ChannelServices.RegisterChannel(channel,false);
             RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "HelloWorld", WellKnownObjectMode.SingleCall);
             System.Console.WriteLine("Press Any Key to Exit ! ");
             System.Console.ReadLine();
         }
     }
}

 

上面代码中可以用

ChannelServices.RegisterChannel(channel);

ChannelServices.RegisterChannel(channel,false);
但是在.NET Framework 2.0中编译时会提示

'System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(System.Runtime.Remoting.Channels.IChannel)' is obsolete: 'Use System.Runtime.Remoting.ChannelServices.RegisterChannel(IChannel chnl, bool ensureSecurity) instead.'

原因是是.NET Framework 2.0新增的函数

System.Runtime.Remoting.ChannelServices.RegisterChannel(IChannel chnl, bool ensureSecurity)


其中参数:ensureSecurity
如果启用了安全,则为 true;否则为 false。将该值设置为 false 将不会使在 TCP 或 IPC 信道上所做的安全设置无效。

此外MSDN中注释:对于 TcpServerChannel,将 esureSecurity 设置为 true 将在 Win98 上引发异常(因为 Wi9x 上不支持安全 tcp 信道);对于 Http 服务器信道,这样会在所有平台上引发异常(如果想要安全的 http 信道,用户需要在 IIS 中承载服务)。

3、创建客户机。需要引用System.Runtime.Remoting程序集和之前创建的RemoteHello.dll程序集
      在此创建名为HelloClient的Console Application

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;

namespace HelloClient
{
     class HelloClient
     {
         static void Main(string[] args)
         {
            
             ChannelServices.RegisterChannel(new TcpClientChannel(), false);
             Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8085/Hi");
");
             if (obj == null)
             {
                 Console.WriteLine("False to Link Server.");
                 return;
             }
             for (int i = 0; i < 6; i++)
             {
                 Console.WriteLine(obj.HelloWorld("MadRam.neo"));
             }
         }
     }
}
  
注:
1、本实例在客户端运行HelloClient,但是要在服务器上远程对象注册之后,使服务器上的HelloClient服务一直处于运行状态,直到按任意键为止,否则会出错!
2、将代码中的“tcp://localhost:8085/Hi"”换成其他网址就可以运行在网络上,比如换成:tcp://192.168.3.235:8085/Hi"。
3、能作为远程服务的只能是dll,exe可不行;