.Net Remoting Hello world 示例(接口版)

 .Net Remoting 提供 .Net 应用高效的分布式运算能力,一个最简单的示例便于了解其使用机制。

  使用接口,因为可以使用其它的实现替换现有模块,而使用它的模块却不需要做任何修改,所以,这种方式可以提供更灵活的实现,所以写了一个 Hello world 应用,以为示例。

  对于大多数 .Net Remoting 应用,都需要有一个公共项目,同时被客户端和服务器端引用,在这个例子中,公共项目中只有一个接口 ICommand :

using System;

namespace Sample.Remoting
{
  public interface ICommand
  {
    void Run(string Command);
  }
}


  对于服务器端,需要有一个实现此接口的类,在这里实现的是 ConsoleOutCommand,为了让它在服务器端,而不是客户端运行,必须从 MarshalByRefObject 继承:

using System;

namespace Sample.Remoting
{
  public class ConsoleOutCommand : MarshalByRefObject, ICommand
  {
    public ConsoleOutCommand()
    {
    }

    public void Run(string Command)
    {
      Console.WriteLine(Command);
    }
  }
}


  另外,服务器端需要注册成一个 Remoting 的服务器,当然,根据需要,可以分别注册成 SingleCall 或者 Singleton,在这里使用的是 SingleCall:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Sample.Remoting
{
  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
      TcpChannel chan = new TcpChannel(8085);
      ChannelServices.RegisterChannel(chan);
      RemotingConfiguration.RegisterWellKnownServiceType(typeof(ConsoleOutCommand), "Sample", WellKnownObjectMode.SingleCall);
      System.Console.WriteLine("Hit <enter> to exit...");
      Console.ReadLine();
    }
  }
}


  而客户端,只需要通过 Remoting 协议,取得这个对象的代理就可以使用它了:

using System;
using System.Runtime.Remoting;

namespace Sample.Remoting
{
  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
      string url = "tcp://localhost:8085/Sample";
      Type type = typeof(ICommand);
      ICommand Command = (ICommand)Activator.GetObject(type, url);
      Command.Run("Remoting out!!!");
    }
  }
}


  这样,这个例子就可以运行了。

  另外,如果要传送的参数是一个自定义的类,则必须保证它是可序列化的,可以通过标记成 Serializable 或者继承 ISerializable 接口,一般来说,标记成 Serializable 就可以了:

[Serializable]
public class Student
{
  public string Name;
  public string Password;
  public int Age;
}

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值