zoukankan      html  css  js  c++  java
  • 初识.Net Remoting

    初识.Net Remoting
    .Net对于远程调用提供了两种方法:Remoting和WebService。WebService现在是如火如荼
    优点:
    1、Tcp通道的Remoting速度非常快
    2、虽然是远程的,但是非常接近于本地调用对象
    3、可以做到保持对象的状态
    4、没有应用程序限制,可以是控制台,winform,iis,windows服务承载远程对象
    缺点:
    1、不是标准的应用,因此有平台限制(不能想Webservice那样可以跨平台,只要Webservice内部方法能够返回json或者xml数据即可)
    2、脱离iis的话需要有自己的安全机制
    可以看出来,比起WebService,Remoting更适合于中小型局域网应用,而不适用于企业级的应用。为了更加深入地理解Remoting的实现原理,
    这里提供一个很简单的实例:

    创建解决方案
    为了编写Remoting实例,我们需要创建一个解决方案,名字叫做SimpleRemoting,添加第一个项目工程用于创建实例类以及简单的方法,这个工程我们取名也叫做:SimpleRemoting,然后在工程内创建一个Hello.cs文件,
    但是这个类需要继承MarshalByRefObject类,一次来标记用于远程调用。核心代码如下所示:
    /// <summary>
    /// Hello类需要继承MarshalByRefObject作为远程调用的对象
    /// </summary>
    public class Hello:MarshalByRefObject
    {
    /// <summary>
    /// 构造函数
    /// </summary>
    public Hello()
    {
    Console.WriteLine("Constructor called");
    }

    /// <summary>
    /// 析构函数
    /// </summary>
    ~Hello()
    {
    Console.WriteLine("Destructor called");
    }

    /// <summary>
    /// 根据姓名参数打印问候语
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public string HelloWorld(string name)
    {
    Console.WriteLine("Hello World!");
    return "Hi," + name;
    }
    }

    二、创建Remoting服务端
    在上面的解决方案内继续添加Remoting服务端的控制台应用程序(在添加工程的弹出框内选择左侧的”Visual C#"节点,然后右侧会有控制台应用程序一项),
    取名叫做:Server。接着引用工程SimpleRemoting和添加System.Runtime.Remoting的dll,这个主要是为了可以使用管道。核心代码如下所示:
    /// <summary>
    /// Remoting 服务端
    /// </summary>
    public class HelloServer
    {
    static void Main(string[] args)
    {
    //创建TCP管道
    TcpServerChannel channel = new TcpServerChannel(6666);
    //注册管道
    ChannelServices.RegisterChannel(channel, false);

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "MyObject", WellKnownObjectMode.SingleCall);

    System.Console.WriteLine("按下任何键退出!");

    System.Console.ReadLine();
    }
    }

    创建Remoting客户端
    在上面的解决方案内继续添加Remoting客户端的控制台应用程序(在添加工程的弹出框内选择左侧的”Visual C#"节点,然后右侧会有控制台应用程序一项),取名叫做:Client。
    接着引用工程SimpleRemoting和添加System.Runtime.Remoting的dll,这个主要是为了可以使用管道。核心代码如下所示:
    /// <summary>
    /// Remoting的客户端
    /// </summary>
    public class HelloClient
    {
    static void Main(string[] args)
    {
    //注册管道
    ChannelServices.RegisterChannel(new TcpClientChannel(), false);
    //远程调用Remoting服务
    Hello hello = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:6666/MyObject");

    if (hello == null)
    {
    Console.WriteLine("连接Remoting服务失败!");
    return;
    }
    Console.WriteLine(hello.HelloWorld("aganar"));
    }
    }

    四、运行程序
    1、先编译整个解决方案成功后然后先运行Server工程,开启Remoting服务监听程序;
    2、运行Client客户端工程,通过TCP管道去获取实例对象然后执行实例的某些方法或者属性。

  • 相关阅读:
    centos crash debug
    go get Unknown SSL protocol error in connection to gopkg.in
    Tensorflow serving with Kubernetes
    Spring 集成 Swagger UI
    Docker Registry V2 Garbage Collection
    Docker Registry V2 with Nginx
    Zabbix磁盘性能监控
    Zabbix CPU utilization监控参数
    Windows挂载Gluster复制卷
    Redis持久化存储(三)
  • 原文地址:https://www.cnblogs.com/hbsfgl/p/3853712.html
Copyright © 2011-2022 走看看