首页 > Web开发 > 详细

. Net Remoting 远程对象 配置文件方式

时间:2015-12-05 11:14:33      阅读:193      评论:0      收藏:0      [点我收藏+]

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文件复制到次应用程序执行文件目录中,并在项目中引用。

 

调用结果:

技术分享

服务端

 

 技术分享

  

技术分享

客户端

技术分享

 

. Net Remoting 远程对象 配置文件方式

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!