remoting 的基本用法

今天学习了REMOTING的基本用法:(总结如下:)

(1)客户端: client

using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
using System.IO;
namespace RemotingToAdd
{
    class client
    {
        static void Main(string[] args)
        {
            TcpChannel tcpchannel = new TcpChannel();
            ChannelServices.RegisterChannel(tcpchannel);

            Add addmethod = (Add)Activator.GetObject(typeof(Add), "tcp://localhost:8080/add");

            if (addmethod == null)
                Console.WriteLine(
                    "Could not locate TCP server");

            HttpChannel httpchannel = new HttpChannel();
            //注册具有通道服务的 通道
            ChannelServices.RegisterChannel(httpchannel);

            Add addmethod1 = (Add)Activator.GetObject(typeof(Add), "http://localhost:8081/add");
            //RemotingConfiguration.Configure(@"Client.exe.config");
         
            //Add addmethod1 = new Add();
            if(addmethod1==null)
                Console.WriteLine(
                    "Could not locate HTTP server");

            Console.WriteLine("tcp is{0}",addmethod.add(1,2));
            Console.WriteLine("http is {0}",addmethod1.add(3.2,3.2));
            Console.ReadKey();
        }
    }
}

 

(2)客户端配置文件APP.CONFIG

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <client>
        <wellknown type="RemotingToAdd.Add,General" url="http://localhost:8081/add" />
      </client>
      <channels>
        <channel ref="http" port="0"></channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

 

(3) 中间类 add.cs

  using System;

namespace RemotingToAdd
{
    public class Add :MarshalByRefObject
    {
        public Add()
        {
            Console.WriteLine("Add activated!");
        }
        public double add(double a, double b)
        {
            return a + b;
        }
    }
}
(4)服务器端: server.cs

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System;

namespace RemotingToAdd
{
    class server
    {
        static void Main(string[] args)
        {
            //实例化Tcp通讯通道。
            TcpChannel tcpchannel = new TcpChannel(8080);
          
            //实例化HTTP通讯通道
            HttpChannel httpchannel = new HttpChannel(8081);
             //本地注册Tcp Http通道
            ChannelServices.RegisterChannel(tcpchannel);
            ChannelServices.RegisterChannel(httpchannel);
        
             //呼叫并处理该方法;
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Add), "add", WellKnownObjectMode.Singleton);
            //RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            System.Console.WriteLine("Press Enter key to exit");
            System.Console.ReadLine();
        }
    }
}

(5)服务器端配置app.config

<!--

在服务器配置文件中,最外层的元素是<configuration>,这是所有配置文件的共性(包括Web.config配置文件)。

所有的远程配置项必须作为子元素添加到<system.runtime.remoting>下面。

<application>元素使用name属性指定了服务器的名称,该应用程序提供了服务,并请求了服务的通道配置。

应用程序所提供的服务必须作为<service>的子元素列出,这就是远程对象本身,可以使用<wellknown>元素来指定远程对象,mode属性可以指定为SingleCall或Singleton,在后面我们会说到。同时用type属性来指定已经定义了类型的对象,只需要指定程序集的名称即可,不需要扩展名DLL。

在<channels>元素中,我们定义了服务器要使用的通道,用ref属性可以引用一个预先定义好的通道,同时必须使用port属性为通道分配端口,因为服务器必须有一个客户机所熟知的端口号,以便客户机可以利用该端口号。这些通道在机器配置文件中已经定义预先定义了6个,我们可以打开Machine.config文件看一下,默认的路径为%SystemRoot%/Microsoft.NET/Framework/<vx.x.x>/CONFIG。


-->

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode="Singleton" type="General,RemotingToAdd.Add" objectUri="add"/>
      </service>
      <channels>
        <channel port ="8081" ref="http"></channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

接下来转载:http://terrylee.cnblogs.com/archive/2005/11/17/278366.html

 使用.NET Remoting开发分布式应用——配置文件篇

作者:Terrylee

 

我们已经知道可以通过编码的方式配置服务器通道和远程客户机,除此之外,还可以使用配置文件对服务器通道和远程客户机进行配置。使用远程客户机和服务器对象的配置文件的优点在于,用户无需修改任何一行代码,也无需进行重新编译,便可以配置通道和远程对象。

.NET提供了Remoting配置文件的标准,基于XML格式。

 

一.配置文件

1.服务器配置文件:

先来看一个服务器配置文件的实例,然后我再具体解释一下其中的内容:

 

<configuration>,这是所有配置文件的共性(包括Web.config配置文件)。

 1 <? xml version="1.0" encoding="utf-8"  ?>
 2 < configuration >
 3      < system .runtime.remoting >
 4          < application >
 5              < service >
 6                  < wellknown 
 7                      mode ="Singleton"  
 8                     type ="RemotingConfigDemo.HelloServer, General"  
 9                     objectUri ="SayHello"   />
10              </ service >
11              < channels >
12                  < channel  port ="8086"  ref ="http" />
13              </ channels >
14          </ application >
15      </ system.runtime.remoting >
16 </ configuration >
17

在服务器配置文件中,最外层的元素是

所有的远程配置项必须作为子元素添加到<system.runtime.remoting>下面。

<application>元素使用name属性指定了服务器的名称,该应用程序提供了服务,并请求了服务的通道配置。

应用程序所提供的服务必须作为<service>的子元素列出,这就是远程对象本身,可以使用<wellknown>元素来指定远程对象,mode属性可以指定为SingleCallSingleton,在后面我们会说到。同时用type属性来指定已经定义了类型的对象,只需要指定程序集的名称即可,不需要扩展名DLL

<channels>元素中,我们定义了服务器要使用的通道,用ref属性可以引用一个预先定义好的通道,同时必须使用port属性为通道分配端口,因为服务器必须有一个客户机所熟知的端口号,以便客户机可以利用该端口号。这些通道在机器配置文件中已经定义预先定义了6个,我们可以打开Machine.config文件看一下,默认的路径为%SystemRoot%/Microsoft.NET/Framework/<vx.x.x>/CONFIG

2.客户机配置文件:

典型的客户机配置文件如下所示:

 1 <? xml version="1.0" encoding="utf-8"  ?>
 2 < configuration >
 3      < system .runtime.remoting >
 4          < application >
 5              < client >
 6                  < wellknown  type ="RemotingConfigDemo.HelloServer, General"  url ="http://localhost:8086/SayHello"   />
 7              </ client >
 8              < channels >
 9                  < channel  ref ="http"  port ="0" ></ channel >
10              </ channels >
11          </ application >
12      </ system.runtime.remoting >
13 </ configuration >
14

 

同服务器配置文件的元素一样,不同的是这次是客户机通道,所以它不需要指定端口号,我们可以暂时指定为0号。其他的保持不变。

 

二.示例程序

1.远程对象代码:

 1 using  System;
 2 using  System.Text;
 3 using  System.Runtime.Remoting.Lifetime;
 4
 5 namespace  RemotingConfigDemo
 6 {
 7    public class HelloServer : MarshalByRefObject
 8    {
 9        public HelloServer()
10        {
11            Console.WriteLine("服务器激活……");
12        }

13        public String HelloMethod(String name)
14        {
15            Console.WriteLine(
16                "服务器端 :{0}", name);
17            return "这里是:" + name;
18        }

19 
20
21    }

22}

 

2.服务器

配置文件:

 1 <? xml version="1.0" encoding="utf-8"  ?>
 2 < configuration >
 3      < system .runtime.remoting >
 4          < application >
 5              < service >
 6                  < wellknown 
 7                      mode ="Singleton"  
 8                     type ="RemotingConfigDemo.HelloServer, General"  
 9                     objectUri ="SayHello"   />
10              </ service >
11              < channels >
12                  < channel  port ="8086"  ref ="http" />
13              </ channels >
14          </ application >
15      </ system.runtime.remoting >
16 </ configuration >
17

 

服务器代码:

 1 using  System;
 2 using  System.Runtime.Remoting;
 3 using  System.Runtime.Remoting.Channels;
 4 using  System.Runtime.Remoting.Channels.Tcp;
 5 using  System.Runtime.Remoting.Channels.Http;
 6
 7 namespace  RemotingConfigDemo 
 8 {
 9
10    public class Server
11    {
12        public static int Main(string [] args) 
13        {
14            RemotingConfiguration.Configure("Server.exe.config");
15
16            System.Console.WriteLine("按任意键退出……");
17            System.Console.ReadLine();
18            return 0;
19        }

20    }

21}

22

 

3.客户机

配置文件:

 1 <? xml version="1.0" encoding="utf-8"  ?>
 2 < configuration >
 3      < system .runtime.remoting >
 4          < application >
 5              < client >
 6                  < wellknown  type ="RemotingConfigDemo.HelloServer, General"  url ="http://localhost:8086/SayHello"   />
 7              </ client >
 8              < channels >
 9                  < channel  ref ="http"  port ="0" ></ channel >
10              </ channels >
11          </ application >
12      </ system.runtime.remoting >
13 </ configuration >
14

 

客户机代码:

 1 using  System;
 2 using  System.Runtime.Remoting;
 3 using  System.Runtime.Remoting.Channels;
 4 using  System.Runtime.Remoting.Channels.Tcp;
 5 using  System.Runtime.Remoting.Channels.Http;
 6 using  System.IO;
 7
 8 namespace  RemotingConfigDemo 
 9 {
10    public class Client
11    {
12        public static void Main(string[] args)
13        {
14            //使用HTTP通道得到远程对象
15            RemotingConfiguration.Configure("Client.exe.config");
16            HelloServer obj2 = new HelloServer();
17            if (obj2 == null)
18            {
19                System.Console.WriteLine(
20                    "连接HTTP服务器失败……");
21            }

22
23            Console.WriteLine(
24                "Client2 HTTP HelloMethod {0}",
25                obj2.HelloMethod("Caveman2"));
26            Console.ReadLine();
27        }

28    }

29}

30

 

 

三.需要注意的几点

1.程序集的名称常常会和存储程序集的文件的名称相混淆。程序集的名称是HelloServer,而程序集文件的名称是HelloServer.dll。使用方法调用时,需要将程序集的名称作为参数,而不需要使用文件的扩展名。

2.必须将远程对象类的程序集复制到服务程序的可执行文件的目录中,或是通过添加DLL引用。因为通过读取配置文件,将实例化远程框架中的这个远程对象类,程序集必须位于能够被找到的位置。

3.一般来说,我们可以让应用程序的配置文件名和可执行文件的文件名相同,其后跟有文件扩展名.config

4.如果用App.config作为服务器或客户机配置文件,要注意App.config文件在运行后自动变为[应用程序名].exe.config

5.为了防止配置文件找不到,我们可以在项目的属性中设置,在生成后事件里面填写拷贝目录语句:

copy  " $(ProjectDir)/*.config "   " $(TargetDir) "

 

 

如图:

6
.在编码中,可以不要把配置文件名硬编码写死,用如下语句来代替,这是一个很好的编程实践,也是值得推荐的一种写法。7.最后一点,也是最重要的一点,推荐在项目中使用配置文件!

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值