Remoting测试

.NET Remoting 是NET 框架中分布式开发的机制。Remoting 技术使得一个应用程序域中的对象可以访问另外一个应用程序域中的对象,这两个应用程序域可以存在于不同的进程中、不同的主机上、不同的系统上。

Remoting的工作机制 

·客户端调用透明代理暴露的方法

·透明代理对象调用真实代理的方法
·真实代理查找消息接收器链,并把消息发给第一个消息接收器
·消息经过消息接收器链,由最后一个消息接收器发给客户端通道
·客户端通道访问格式化器,序列化消息并且发送
·服务端通道接受到消息,并通过格式化器反序列化消息
·服务器端通道把消息发送给服务器消息接收器链
·服务器消息接收器链把消息发送给对象消息接收器链
·服务器端对象的方法被调用

Demo:

分别采用tcp,http,ipc三种信道

首先如果需要作为远程对象来访问,那么类需要继承System.MarshalByRefObject 

server端代码: 

代码
TcpServerChannel channel = new TcpServerChannel(12345);
                ChannelServices.RegisterChannel(channel, 
true);
                
//使用WellKnown 激活方式, 并且使用SingleCall模式
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer.RemotingTest), "HelloWorld-Tcp", WellKnownObjectMode.SingleCall);
                
// 打印这个信道的名称.
                Console.WriteLine("信道名称{0}.",
                    channel.ChannelName);

                
// 打印这个信道的优先级.
                Console.WriteLine("信道优先级 {0}.",
                    channel.ChannelPriority);

                
//利用 Tcp通道, 并且侦昕12345 端口
                HttpServerChannel httpchannel = new HttpServerChannel(12346);
                ChannelServices.RegisterChannel(httpchannel,
false);
                
//使用WellKnown 激活方式, 并且使用SingleCall模式
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer.RemotingTest), "HelloWorld-Http", WellKnownObjectMode.SingleCall);
                
// 打印这个信道的名称.
                Console.WriteLine("信道名称 {0}.",
                    httpchannel.ChannelName);

                
// 打印这个信道的优先级.
                Console.WriteLine("信道优先级 {0}.",
                    httpchannel.ChannelPriority);

            
                
// 创建一个IPC信道
                Hashtable ht = new Hashtable();
                ht[
"portName"= "ipchelloword";
                ht[
"name"= "ipc";
                ht[
"authorizedGroup"= "Users";
                IpcChannel serverChannel 
= new IpcChannel(ht, nullnull);
                
// 注册这个IPC信道.
                System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(serverChannel,false);

                
// 打印这个信道的名称.
                Console.WriteLine("信道名称{0}.",
                    serverChannel.ChannelName);

                
// 打印这个信道的优先级.
                Console.WriteLine("信道优先级{0}.",
                    serverChannel.ChannelPriority);

                
// 打印这个信道的URI数组.
                System.Runtime.Remoting.Channels.ChannelDataStore channelData = (System.Runtime.Remoting.Channels.ChannelDataStore)serverChannel.ChannelData;
                
                
// 向信道暴露一个远程对象.
                System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer.RemotingTest), "helloword", System.Runtime.Remoting.WellKnownObjectMode.Singleton);

                Console.ReadLine();

client端代码

代码
    //使用TCP通道连接
                
                TcpClientChannel tcpchannel 
= new TcpClientChannel();
                ChannelServices.RegisterChannel(tcpchannel, 
true);
                
//获得远程对象
                RemotingServer.RemotingTest helloworld = (RemotingServer.RemotingTest)Activator.GetObject(
                
typeof(RemotingServer.RemotingTest),
                
"tcp://localhost:12345/Helloworld-Tcp");
                
//调用远程对象方法
                
//helloworld.SayHello("张三");

                HttpClientChannel httpchannel 
= new HttpClientChannel();
                ChannelServices.RegisterChannel(httpchannel, 
false);
                
//获得远程对象
                RemotingServer.RemotingTest helloworldhttp = (RemotingServer.RemotingTest)Activator.GetObject(
                
typeof(RemotingServer.RemotingTest),
                
"http://localhost:12346/Helloworld-Http");
                
//IpcClientChannel ipcchannel = new IpcClientChannel();
                
//ChannelServices.RegisterChannel(ipcchannel, false);
                ////获得远程对象
                //RemotingServer.RemotingTest helloworldipc = (RemotingServer.RemotingTest)Activator.GetObject(
                
//typeof(RemotingServer.RemotingTest),
                
//"ipc://TestChannel/Helloworld-Ipc");
                
//调用远程对象方法
                helloworld.SayHello("tcpchannel");
                helloworldhttp.SayHello(
"httpchannel");
                
//helloworldipc.SayHello("icp");

                
// 创建一个IPC信道。
                IpcClientChannel channel = new IpcClientChannel();

                
// 注册这个信道。
                System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel,false);

                
// 注册一个远程对象的客户端代理.
                System.Runtime.Remoting.WellKnownClientTypeEntry remoteType = new System.Runtime.Remoting.WellKnownClientTypeEntry(typeof(RemotingServer.RemotingTest), "ipc://ipchelloword/helloword");
                System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(remoteType);

                RemotingServer.RemotingTest service 
= new RemotingServer.RemotingTest();
                service.SayHello(
"ipc");
原文地址:https://www.cnblogs.com/dlz1123/p/1962055.html