zoukankan      html  css  js  c++  java
  • .NET Remoting(一)

    今天实习中用到.NET Remoting,总结下:

    需要建立四个工程:RemotingObject    RemotingServerObject     RemotingServer   RemotingClient

    1.RemotingObject:

    IOperation.cs

    namespace RemotingObject
    {
    public interface IOperation
    {
    int Add(int a, int b);
    int Subtract(int a,int b);
    }
    }

    2.RemotingServerObject 

    Operation.cs

    using System;
    using RemotingObject;

    namespace RemotingServerObject
    {
    public class Operation:MarshalByRefObject,IOperation
    {
    public int Add(int a, int b)
    {
    return a + b;
    }
    public int Subtract(int a, int b)
    {
    return a - b;
    }
    }
    }

    3.RemotingServer 

    TestClass.cs

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

    namespace RemotingServer
    {
    class TestClass
    {
    static void Main(string[] args)
    {
    TcpChannel tcpChannel = new TcpChannel(8888);
    ChannelServices.RegisterChannel(tcpChannel, false);
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServerObject.Operation), "Operation.soap", WellKnownObjectMode.SingleCall);

    // the server will keep running until key press.
    Console.ReadKey();
    }

    }
    }


    4.RemotingClient

    TestClass.cs

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

    namespace RemotingClient
    {
    class TestClass
    {
    static void Main(string[] args)
    {
    TcpChannel tcpChannel = new TcpChannel();
    ChannelServices.RegisterChannel(tcpChannel,false);

    IOperation operation = (IOperation)Activator.GetObject(typeof(IOperation), "tcp://localhost:8888/Operation.soap");
    int result_Add = operation.Add(1,2);
    int result_Subtract = operation.Subtract(4, 3);
    Console.WriteLine(result_Add);
    Console.WriteLine(result_Subtract);
    }
    }
    }


     先运行RemotingServer,再运行RemotingClient

    运行结果:

    3
    1
    请按任意键继续. . .

    理论基础:

    (1).NET Remoting 基础

    一、先阐述下什么是Remoting

    在介绍Remoting的经典书《Advanced .NET Remoting》中,写到“Remoting is the process of programs or components interacting across certain boundaries”,Remoting是一种分布式处理方式,在.NET Framework下,Remoting 就是 DCOM 的一种升级,甚至可以说it simply replaces DCOM.Remoting 提供了跨进程的通信。

    二、Remoting中有三个主体:远程对象(Remoting Object)   服务端(Server) 客户端(Client)

    远程对象(Remoting Object)

    远程对象一般分为两个部分实现,一个是提供公共接口,另一个是具体实现接口。一般服务端与客户端均引入公共接口,而具体实现接口只在服务端引入。

     服务端(Server)

    在这里一般进行三步:

    1.注册通道

    TcpChannel tcpChannel = new TcpChannel(8888);
    ChannelServices.RegisterChannel(tcpChannel, false);


    其中8888为端口

    2.注册远程对象

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServerObject.Operation), "Operation.soap", WellKnownObjectMode.SingleCall);


    其中RemotingServerObject.Operation是要注册的对象,Operation.soap是服务端uri WellKnownObjectMode.SingleCall是服务端激活模式,服务端激活模式有两种,一种是WellKnownObjectMode.SingleCall,另一种是WellKnownObjectMode.SingleTon。这两种激活模式的区别:SingleCall模式时,Remoting为每一个客户端建立一个远程实例,也就是当运行多个客户端时,彼此是独立的;SingleTon模式时,则 Remoting 将为所有客户端建立同个对象实例,也就是当时运行多个客户端时,这次运行的客户端用的对象实例都是第一次运行时的。

    3.注销通道

    如果要关闭 Remoting 的服务, 则需要注销通道,否则如果你想再一次注册该通道,会抛出异常。


    客户端(Client)

    在这里一般进行两步:

    1.注册通道

    TcpChannel tcpChannel = new TcpChannel();
    ChannelServices.RegisterChannel(tcpChannel,false);


    2.获得远程对象

    IOperation operation = (IOperation)Activator.GetObject(typeof(IOperation),    "tcp://localhost:8888/Operation.soap");

    其中tcp://localhost:8888/Operation.soap是服务器端的uri,必须是上面的uri一致

  • 相关阅读:
    spring boot + activeMq 邮件服务
    spring boot集成activeMQ
    Java做爬虫也很方便
    Go语言学习03
    MongoDB学习-->Gridfs分布式存储&DBRef关联查询
    tomcat与oracle关于8080端口的冲突
    端口号的查找处理方法
    很不错的在线Office控件:IWebOffice与SOAOffice
    SOAOffice和iWebOffice、NTKO的比较及其优势(转)
    Cannot get a connection, pool exhausted解决办法
  • 原文地址:https://www.cnblogs.com/danshui/p/2293644.html
Copyright © 2011-2022 走看看