. Net Remoting体系结构(1) 远程对象 配置文件方式

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         <!--知名对象-->
 6         <!--<wellknown mode="SingleCall" type="Shen.RemoteTest.MyRemoteObject,MyRemoteObject" objectUri="MyRemoteObject"/>-->
 7         <!--客户端激活对象-->
 8         <activated type="Shen.RemoteTest.MyRemoteObject,MyRemoteObject"/>
 9       </service>
10       <channels>
11         <channel ref="tcp" port="9000"/>
12       </channels>
13     </application>
14   </system.runtime.remoting>
15 </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 url="tcp://localhost:9000">
 5         <!--知名对象-->
 6         <!--<wellknown type="Shen.RemoteTest.MyRemoteObject,MyRemoteObject" url="tcp://localhost:9000/MyRemoteObject"/>-->
 7         <!--客户端激活对象-->
 8         <activated type="Shen.RemoteTest.MyRemoteObject,MyRemoteObject" />
 9       </client>
10     </application>
11   </system.runtime.remoting>
12 </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文件复制到此应用程序执行文件目录中,并在项目中引用。

调用结果:

服务端

 

  

客户端

原文地址:https://www.cnblogs.com/shenshiting/p/5021167.html