一 远程对象 /**/ /* * 用于远程调用的类,此类必须继承于 MarshalByRefObject */ public class Hello : MarshalByRefObject ... { private int count = 0; public string SayHello(string name) ...{ return "Hello, " + name + "!"; } public int GetCount() ...{ return ++count; } } 二 Server端 class Server ... { static void Main(string[] args) ...{ // 使用 Server Activation 方式 //HelloServerActivated(); // 使用 Client Activation 方式 HelloClientActivated(); //EmployeeClientActivated(); Console.Read(); } // class Hello Server Activation 方式,有 SingleCall 和 Singleton 两种方式 public static void HelloServerActivated() ...{ try ...{ // 使用 TCP 通道 TcpChannel tcp = new TcpChannel(9999); ChannelServices.RegisterChannel(tcp, false); // 使用 IPC 通道 //IpcChannel ipc = new IpcChannel("demo"); //ChannelServices.RegisterChannel(ipc, false); // 使用 HTTP 通道 //HttpChannel http = new HttpChannel(10000); //ChannelServices.RegisterChannel(http, false); RemotingConfiguration.RegisterWellKnownServiceType( typeof(RemoteObject.Hello), "Hello", WellKnownObjectMode.SingleCall); Console.WriteLine("Hello class is registered on server-side..."); } catch (Exception ex) ...{ Console.WriteLine(ex.ToString()); } } // class Hello Client Activation 方式 public static void HelloClientActivated() ...{ try ...{ // 使用 TCP 通道 TcpChannel tcp = new TcpChannel(9999); ChannelServices.RegisterChannel(tcp, false); RemotingConfiguration.RegisterActivatedServiceType(typeof(RemoteObject.Hello)); Console.WriteLine("Hello class is registered on server-side..."); } catch (Exception ex) ...{ Console.WriteLine(ex.ToString()); } } // class Employee Client Activation 方式 public static void EmployeeClientActivated() ...{ try ...{ // 使用 TCP 通道 TcpChannel tcp = new TcpChannel(9999); ChannelServices.RegisterChannel(tcp, false); RemotingConfiguration.RegisterActivatedServiceType(typeof(RemoteObject.Employee)); Console.WriteLine("Employee class is registered on server-side..."); } catch (Exception ex) ...{ Console.WriteLine(ex.ToString()); } } } 三 客户端 class Client ... { static void Main(string[] args) ...{ // 使用 Server Activation 方式 //HelloServerActivated(); // 使用 Client Activation 方式 HelloClientActivated(); //EmployeeClientActivated(args); Console.Read(); } // class Hello Server Activation 方式 public static void HelloServerActivated() ...{ try ...{ // 使用 TCP 通道 Hello hello = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:9999/Hello"); // 使用 IPC 通道 //Hello hello = (Hello)Activator.GetObject(typeof(Hello), "ipc://demo/Hello"); // 使用 HTTP 通道 //Hello hello = (Hello)Activator.GetObject(typeof(Hello), "http://localhost:10000/Hello"); if (hello == null) Console.WriteLine("could not locate server."); else ...{ Console.WriteLine(hello.SayHello("Michael Jordan")); Console.WriteLine(hello.GetCount()); Console.WriteLine(hello.GetCount()); Console.WriteLine(hello.GetCount()); } } catch (Exception ex) ...{ Console.WriteLine(ex.ToString()); } } // class Hello Client Activation 方式 public static void HelloClientActivated() ...{ try ...{ object[] url = ...{ new UrlAttribute("tcp://localhost:9999") }; Hello hello = (Hello)Activator.CreateInstance(typeof(Hello), null, url); // 第二个参数是传入的构造函数的参数,null指不传入参数 if (hello == null) Console.WriteLine("could not locate server."); else ...{ Console.WriteLine(hello.SayHello("Michael Jordan")); Console.WriteLine(hello.GetCount()); Console.WriteLine(hello.GetCount()); Console.WriteLine(hello.GetCount()); } } catch (Exception ex) ...{ Console.WriteLine(ex.ToString()); } } // class Employee Client Activation 方式 public static void EmployeeClientActivated(string[] input) ...{ try ...{ object[] args = input; object[] url = ...{ new UrlAttribute("tcp://localhost:9999") }; Employee emp = (Employee)Activator.CreateInstance(typeof(Employee), args, url); if (emp == null) Console.WriteLine("could not locate server."); else ...{ Console.WriteLine("Your name is " + emp.GetName()); } } catch (Exception ex) ...{ Console.WriteLine(ex.ToString()); } } }