Remoting简单实践

一句话概括

  remoting是微软的一种实现在不同的.net应用程序中进行分布式通信的技术

重要概念

  原理大致是首先客户端通过remoting通道来获取服务器对象代理,通过序列化与反序列方式实现数据交互

  远程对象:服务器端的实现类必须继承MarshalByRefObject,进而实现remoting通信,支持跨程序域的访问

  远程对象的激活

  主要分为服务器端激活和客户端激活

  交互前都需要激活相应的实例,便于调用方法
  通道:
  主要tcp,http,ipc这几种方式
  tcp通过二进制传输,传输效率高,局域网中适合用tcp

  http:采用soap格式序列化消息对象,可以跨越防火墙,安全性较高,

  IpcChannel:进程间通信,只使用同一个系统进程之间的通信,不需要主机名和端口号。而使用Http通道和Tcp通道都要指定主机名和端口号。

简单Demo

  1.创建服务器端服务实现类

 public class MyRemotingObject : MarshalByRefObject
    {
        // 用来测试Tcp通道 
        public int AddForTcpTest(int a, int b)
        {
            return a + b;
        }

        // 用来测试Http通道
        public int MinusForHttpTest(int a, int b)
        {
            return a - b;
        }

        // 用来测试IPC通道
        public int MultipleForIPCTest(int a, int b)
        {
            return a * b;
        }
    }

  2.配置服务端remting服务 

<?xml version="1.0" encoding="utf-8" ?>
<!--服务端App.config的内容-->
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown  mode="Singleton"
                    type="RemotingDemo.MyRemotingObject,RemotingDemo"
                    objectUri="MyRemotingObject"/>
      </service>
      <channels>
        <channel port="9001" ref="tcp"/>
        <channel port="9002" ref="http"/>
        <channel portName="IpcTest" ref="ipc"/>
        <!--Ipc通道不需要端口号-->
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

 3.在服务器端程序入口启动加载配置文件,让它内部自己去注册启动服务

 RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);

4.客户端激活远程对象并调用相应方法

   MyRemotingObject proxyobj1 = Activator.GetObject(typeof(MyRemotingObject), "tcp://localhost:9001/MyRemotingObject") as MyRemotingObject;
            if (proxyobj1 == null)
            {
                Console.WriteLine("连接TCP服务器失败");
            }

            //HttpChannel httpChannel = new HttpChannel();
            //ChannelServices.RegisterChannel(httpChannel, false);
            MyRemotingObject proxyobj2 = Activator.GetObject(typeof(MyRemotingObject), "http://localhost:9002/MyRemotingObject") as MyRemotingObject;
            if (proxyobj2 == null)
            {
                Console.WriteLine("连接Http服务器失败");
            }

            //IpcChannel ipcChannel = new IpcChannel();
            //ChannelServices.RegisterChannel(ipcChannel, false);
            MyRemotingObject proxyobj3 = Activator.GetObject(typeof(MyRemotingObject), "ipc://IpcTest/MyRemotingObject") as MyRemotingObject;
            if (proxyobj3 == null)
            {
                Console.WriteLine("连接Ipc服务器失败");
            }
            // 输出信息
            Console.WriteLine("This call object by TcpChannel, 100 + 200 = {0}", proxyobj1.AddForTcpTest(100, 200));
            Console.WriteLine("This call object by HttpChannel, 100 - 200 = {0}", proxyobj2.MinusForHttpTest(100, 200));
            Console.WriteLine("This call object by IpcChannel, 100 * 200 = {0}", proxyobj1.MultipleForIPCTest(100, 200));
            Console.WriteLine("Press any key to exit!");
            Console.ReadLine();

 总结

    以前很惧怕去了解相关的技术,感觉非常难学,譬如说wcf,接下来我应该去实践wcf带来的乐趣!

    

原文地址:https://www.cnblogs.com/JaggerMan/p/4563642.html