.NET Remoting 最简单示例

来源:互联网 发布:js特效模板 编辑:程序博客网 时间:2024/05/21 00:00

原文:http://blog.csdn.net/kissqw/article/details/18605655


笔记:

主要步骤

1. 创建remote object, 此object继承自MarshalByRefObject

2. 服务端,注册通道,注册remote service type

3. 客户端,注册通道,获得服务端 service type对象,调用对象方法


学习技术知识一个好的方法是先动手,再深入,

给出一个最简单的Remoting程序示例(C#)如下:


Step1:创建类库(DLL)工程RemotingObjects,类Person代码如下:

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace RemotingObjects  
  8. {  
  9.     public interface IPerson  
  10.     {  
  11.         String getName(String name);  
  12.   
  13.     }  
  14.   
  15.     public class Person : MarshalByRefObject, IPerson  
  16.     {  
  17.         public Person()  
  18.         {  
  19.             Console.WriteLine("[Person]:Remoting Object 'Person' is activated.");  
  20.         }  
  21.   
  22.         public String getName(String name)  
  23.         {  
  24.             return name;  
  25.         }  
  26.     }  
  27. }  
Step2:创建控制台工程RemotingServer(添加项目引用RemotingObjects),类Server代码如下:

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Remoting;  
  5. using System.Runtime.Remoting.Channels;  
  6. using System.Runtime.Remoting.Channels.Tcp;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10.   
  11. namespace RemotingServer  
  12. {  
  13.     class Server  
  14.     {  
  15.         static void Main(string[] args)  
  16.         {  
  17.             TcpChannel channel = new TcpChannel(8080);  
  18.             ChannelServices.RegisterChannel(channel, false);  
  19.             RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObjects.Person), "RemotingPersonService", WellKnownObjectMode.SingleCall);  
  20.   
  21.             System.Console.WriteLine("Server:Press Enter key to exit");  
  22.             System.Console.ReadLine();  
  23.         }  
  24.     }  
  25. }  

Step3:创建控制台工程RemotingClient(添加项目引用RemotingObjects及必要类库),类Client代码如下:

(PS:正式应用开发,不需要也不应该直接引用RemotingObjects类库,而应该引用相关Remoting类的接口库。)

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using RemotingObjects;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Runtime.Remoting.Channels;  
  6. using System.Runtime.Remoting.Channels.Tcp;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10. namespace RemotingClient  
  11. {  
  12.     class Client  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             TcpChannel channel = new TcpChannel();  
  17.             ChannelServices.RegisterChannel(channel, false);  
  18.             IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), "tcp://localhost:8080/RemotingPersonService");  
  19.             if (obj == null)  
  20.             {  
  21.                 Console.WriteLine("Couldn't crate Remoting Object 'Person'.");  
  22.             }  
  23.   
  24.             Console.WriteLine("Please enter your name:");  
  25.             String name = Console.ReadLine();  
  26.             try  
  27.             {  
  28.                 Console.WriteLine(obj.getName(name));  
  29.             }  
  30.             catch (System.Net.Sockets.SocketException e) {  
  31.                 Console.WriteLine(e.ToString());  
  32.             }  
  33.                 Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  
Step4:运行编译出的EXE:RemotingServer.exe和RemotingClient.exe,查看运行结果。
0 0