1 编写远程对象 MyRemoteObject.dll
1 namespace Shen.RemoteTest 2 { 3 public class MyRemoteObject:System.MarshalByRefObject 4 { 5 public MyRemoteObject() 6 { 7 System.Console.WriteLine("MyRemoteObject构造函数被调用"); 8 } 9 public string Hello() 10 { 11 System.Console.WriteLine("Hello MeiMei!"); 12 return "Hello,.Net 客户端程序!"; 13 } 14 } 15 }
2 远程对象服务器宿主应用程序 SimpleServer.exe
a 配置文件 SimpleServer.exe.config
1 <configuration> 2 <system.runtime.remoting> 3 <application> 4 <service> 5 <wellknown mode="SingleCall" type="Shen.RemoteTest.MyRemoteObject,MyRemoteObject" objectUri="MyRemoteObject"/> 6 </service> 7 <channels> 8 <channel ref="http" port="9000"/> 9 </channels> 10 </application> 11 </system.runtime.remoting> 12 </configuration>
b 调用配置文件注册通道
1 using System; 2 using System.Runtime.Remoting; 3 4 namespace SimpleServer 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 RemotingConfiguration.Configure("SimpleServer.exe.config",false); 11 Console.ReadKey(); 12 } 13 } 14 }
将远程对象DLL文件复制到次应用程序执行文件目录中,并在项目中引用。
3 客户端应用程序 SimpleClient.exe
a 配置文件 SimpleClient.exe.config
1 <configuration> 2 <system.runtime.remoting> 3 <application > 4 <client > 5 <wellknown type="Shen.RemoteTest.MyRemoteObject,MyRemoteObject" url="http://localhost:9000/MyRemoteObject"/> 6 </client> 7 </application> 8 </system.runtime.remoting> 9 </configuration>
b 读取配置文件,创建对象,调用远程对象方法。
1 using Shen.RemoteTest; 2 using System; 3 using System.Runtime.Remoting; 4 5 namespace SimpleClient 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 try 12 { 13 RemotingConfiguration.Configure("SimpleClient.exe.config", false); 14 MyRemoteObject obj = new MyRemoteObject(); 15 Console.WriteLine(obj.Hello()); 16 } 17 catch (Exception ex) 18 { 19 Console.WriteLine(ex.Message); 20 } 21 Console.ReadKey(); 22 } 23 } 24 }
将远程对象DLL文件复制到次应用程序执行文件目录中,并在项目中引用。
调用结果:

服务端


客户端

原文:http://www.cnblogs.com/shenshiting/p/5021167.html