• Remoting 我的练习题


    Remoting(简单的测试小程序)

    1、添加一个类库(IRemoting),添加一个接口类(IStudent.cs),有一个方法 (Void Ishow)。

    2、添加一个应用程序窗口(RemotingServices)。

    1、添加引用(.net-system.Runtime-Remoting)(项目—IRemoting[之前建的接口类])

    2、添加一个Student类,导入命名空间(using Iremoting)继承 MarshalByRefObject和IStudent(接口),实现接口,并在该类中写需要执行的语句。

    例:   int Isum = 0;
            public void Ishow()
            {
                for (int i = 0; i < 10; i++)
                {
                    Isum += i;
                }
                Console.WriteLine(Isum);
            }

    3、在主程序中来进行主要操作(Program)

        3.1 导入名称空间

          using System.Runtime.Remoting.Channels;
          using System.Runtime.Remoting;
          using System.Runtime.Remoting.Channels.Http;
          using System.Runtime.Remoting.Channels.Tcp;(Http、Tcp二者可选一)

       3.2 
            /服务器端

              static void Main(string[] args)
              {
                //创建通道
                TcpChannel tcp = new TcpChannel(3000);
                //注册通道
                ChannelServices.RegisterChannel(tcp,false);
                //注册浏览器对象
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(Student),"Student", WellKnownObjectMode.Singleton);
                Console.ReadKey();
            }

    4、添加一个客户端应用程序

        1、添加引用(.net-system.Runtime-Remoting)(项目—IRemoting[之前建的接口类])

       2、导入命名空间

          using System.Runtime.Remoting.Channels;
          using System.Runtime.Remoting;
          using System.Runtime.Remoting.Channels.Http;
          using System.Runtime.Remoting.Channels.Tcp;(Http、Tcp二者可选一)

        3、具体代码实行

           //创建通道
               TcpChannel tcp = new TcpChannel();
                //注册通道
               ChannelServices.RegisterChannel(tcp, false);
                //注册客户端对象
               IStudent stu=    Activator.GetObject(typeof(IStudent), "TCP://10.85.189.35:3000/Student") as IStudent;
               stu.Ishow();

         PS:远程对象激活方式有两种,以上用的是服务器端激活方式(RegisterWellKnownServiceType),客户端激活方式(RegisterActivatedServiceType),服务器端激活方式又叫WellKnow,长翻译为知名对象,服务器激活方式又可分为两种,(SingleTon和singleCall),区别,前者为有状态模式,为每个可恶端建立同一个对象,不管它们是同一个客户端还是其他客户端,后者为无状态模式,当客户端调用远程的方法时Remoting会为客户端建立一个远程对象实例。


    服务器端激活模式

    这里可以不需要引用接口

    1、引用服务端(项目—RemotingServices)

    2、将           IStudent stu=    Activator.GetObject(typeof(IStudent), "TCP://10.85.189.35:3000/Student") as IStudent;
               stu.Ishow();

    这串代码更改为: 

                RemotingConfiguration.RegisterWellKnownClientType(typeof(Student), "TCP://10.85.189.35:3000/Student");
                Student stu = new Student();

  • 相关阅读:
    小小杨的影视空间
    关于励志的事情
    关于2020年的总结
    关于心情不好的时候
    关于我的2020年
    单链表基本操作的实现
    原型模式
    android—安卓系统文件目录结构
    android——apk安装文件的组成结构
    android——项目的组成结构
  • 原文地址:https://www.cnblogs.com/ZhangYuLIng/p/3838647.html
Copyright © 2020-2023  润新知