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

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

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

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

一.配置文件

1.服务器配置文件:

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

 1使用.NET Remoting开发分布式应用——配置文件篇(转载)<?xml version="1.0" encoding="utf-8" ?>
 2使用.NET Remoting开发分布式应用——配置文件篇(转载)<configuration>
 3使用.NET Remoting开发分布式应用——配置文件篇(转载)    <system.runtime.remoting>
 4使用.NET Remoting开发分布式应用——配置文件篇(转载)        <application>
 5使用.NET Remoting开发分布式应用——配置文件篇(转载)            <service>
 6使用.NET Remoting开发分布式应用——配置文件篇(转载)                <wellknown 
 7使用.NET Remoting开发分布式应用——配置文件篇(转载)                    mode="Singleton" 
 8使用.NET Remoting开发分布式应用——配置文件篇(转载)                    type="RemotingConfigDemo.HelloServer, General" 
 9使用.NET Remoting开发分布式应用——配置文件篇(转载)                    objectUri="SayHello" />
10使用.NET Remoting开发分布式应用——配置文件篇(转载)            </service>
11使用.NET Remoting开发分布式应用——配置文件篇(转载)            <channels>
12使用.NET Remoting开发分布式应用——配置文件篇(转载)                <channel port="8086" ref="http"/>
13使用.NET Remoting开发分布式应用——配置文件篇(转载)            </channels>
14使用.NET Remoting开发分布式应用——配置文件篇(转载)        </application>
15使用.NET Remoting开发分布式应用——配置文件篇(转载)    </system.runtime.remoting>
16使用.NET Remoting开发分布式应用——配置文件篇(转载)</configuration>
17使用.NET Remoting开发分布式应用——配置文件篇(转载)

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

所有的远程配置项必须作为子元素添加到<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使用.NET Remoting开发分布式应用——配置文件篇(转载)<?xml version="1.0" encoding="utf-8" ?>
 2使用.NET Remoting开发分布式应用——配置文件篇(转载)<configuration>
 3使用.NET Remoting开发分布式应用——配置文件篇(转载)    <system.runtime.remoting>
 4使用.NET Remoting开发分布式应用——配置文件篇(转载)        <application>
 5使用.NET Remoting开发分布式应用——配置文件篇(转载)            <client>
 6使用.NET Remoting开发分布式应用——配置文件篇(转载)                <wellknown type="RemotingConfigDemo.HelloServer, General" url="http://localhost:8086/SayHello" />
 7使用.NET Remoting开发分布式应用——配置文件篇(转载)            </client>
 8使用.NET Remoting开发分布式应用——配置文件篇(转载)            <channels>
 9使用.NET Remoting开发分布式应用——配置文件篇(转载)                <channel ref="http" port="0"></channel>
10使用.NET Remoting开发分布式应用——配置文件篇(转载)            </channels>
11使用.NET Remoting开发分布式应用——配置文件篇(转载)        </application>
12使用.NET Remoting开发分布式应用——配置文件篇(转载)    </system.runtime.remoting>
13使用.NET Remoting开发分布式应用——配置文件篇(转载)</configuration>
14使用.NET Remoting开发分布式应用——配置文件篇(转载)

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

二.示例程序

1.远程对象代码:

 1使用.NET Remoting开发分布式应用——配置文件篇(转载)using System;
 2使用.NET Remoting开发分布式应用——配置文件篇(转载)using System.Text;
 3使用.NET Remoting开发分布式应用——配置文件篇(转载)using System.Runtime.Remoting.Lifetime;
 4使用.NET Remoting开发分布式应用——配置文件篇(转载)
 5使用.NET Remoting开发分布式应用——配置文件篇(转载)namespace RemotingConfigDemo
 6使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载){
 7使用.NET Remoting开发分布式应用——配置文件篇(转载)    public class HelloServer : MarshalByRefObject
 8使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载)    使用.NET Remoting开发分布式应用——配置文件篇(转载){
 9使用.NET Remoting开发分布式应用——配置文件篇(转载)        public HelloServer()
10使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载)        使用.NET Remoting开发分布式应用——配置文件篇(转载){
11使用.NET Remoting开发分布式应用——配置文件篇(转载)            Console.WriteLine("服务器**……");
12使用.NET Remoting开发分布式应用——配置文件篇(转载)        }

13使用.NET Remoting开发分布式应用——配置文件篇(转载)        public String HelloMethod(String name)
14使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载)        使用.NET Remoting开发分布式应用——配置文件篇(转载){
15使用.NET Remoting开发分布式应用——配置文件篇(转载)            Console.WriteLine(
16使用.NET Remoting开发分布式应用——配置文件篇(转载)                "服务器端 :{0}", name);
17使用.NET Remoting开发分布式应用——配置文件篇(转载)            return "这里是:" + name;
18使用.NET Remoting开发分布式应用——配置文件篇(转载)        }

19使用.NET Remoting开发分布式应用——配置文件篇(转载) 
20使用.NET Remoting开发分布式应用——配置文件篇(转载)
21使用.NET Remoting开发分布式应用——配置文件篇(转载)    }

22使用.NET Remoting开发分布式应用——配置文件篇(转载)}

2.服务器

配置文件:

 1使用.NET Remoting开发分布式应用——配置文件篇(转载)<?xml version="1.0" encoding="utf-8" ?>
 2使用.NET Remoting开发分布式应用——配置文件篇(转载)<configuration>
 3使用.NET Remoting开发分布式应用——配置文件篇(转载)    <system.runtime.remoting>
 4使用.NET Remoting开发分布式应用——配置文件篇(转载)        <application>
 5使用.NET Remoting开发分布式应用——配置文件篇(转载)            <service>
 6使用.NET Remoting开发分布式应用——配置文件篇(转载)                <wellknown 
 7使用.NET Remoting开发分布式应用——配置文件篇(转载)                    mode="Singleton" 
 8使用.NET Remoting开发分布式应用——配置文件篇(转载)                    type="RemotingConfigDemo.HelloServer, General" 
 9使用.NET Remoting开发分布式应用——配置文件篇(转载)                    objectUri="SayHello" />
10使用.NET Remoting开发分布式应用——配置文件篇(转载)            </service>
11使用.NET Remoting开发分布式应用——配置文件篇(转载)            <channels>
12使用.NET Remoting开发分布式应用——配置文件篇(转载)                <channel port="8086" ref="http"/>
13使用.NET Remoting开发分布式应用——配置文件篇(转载)            </channels>
14使用.NET Remoting开发分布式应用——配置文件篇(转载)        </application>
15使用.NET Remoting开发分布式应用——配置文件篇(转载)    </system.runtime.remoting>
16使用.NET Remoting开发分布式应用——配置文件篇(转载)</configuration>
17使用.NET Remoting开发分布式应用——配置文件篇(转载)

服务器代码:

 1使用.NET Remoting开发分布式应用——配置文件篇(转载)using System;
 2使用.NET Remoting开发分布式应用——配置文件篇(转载)using System.Runtime.Remoting;
 3使用.NET Remoting开发分布式应用——配置文件篇(转载)using System.Runtime.Remoting.Channels;
 4使用.NET Remoting开发分布式应用——配置文件篇(转载)using System.Runtime.Remoting.Channels.Tcp;
 5使用.NET Remoting开发分布式应用——配置文件篇(转载)using System.Runtime.Remoting.Channels.Http;
 6使用.NET Remoting开发分布式应用——配置文件篇(转载)
 7使用.NET Remoting开发分布式应用——配置文件篇(转载)namespace RemotingConfigDemo 
 8使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载){
 9使用.NET Remoting开发分布式应用——配置文件篇(转载)
10使用.NET Remoting开发分布式应用——配置文件篇(转载)    public class Server
11使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载)    使用.NET Remoting开发分布式应用——配置文件篇(转载){
12使用.NET Remoting开发分布式应用——配置文件篇(转载)        public static int Main(string [] args) 
13使用.NET Remoting开发分布式应用——配置文件篇(转载)使用.NET Remoting开发分布式应用——配置文件篇(转载)        使用.NET Remoting开发分布式应用——配置文件篇(转载){
14使用.NET Remoting开发分布式应用——配置文件篇(转载)            RemotingConfiguration.Configure("Server.exe.config");
15使用.NET Remoting开发分布式应用——配置文件篇(转载)
16使用.NET Remoting开发分布式应用——配置文件篇(转载)            System.Console.WriteLine("按任意键退出……");
17使用.NET Remoting开发分布式应用——配置文件篇(转载)            System.Console.ReadLine();
18使用.NET Remoting开发分布式应用——配置文件篇(转载)            return 0;
19使用.NET Remoting开发分布式应用——配置文件篇(转载)        }

20使用.NET Remoting开发分布式应用——配置文件篇(转载)    }

21使用.NET Remoting开发分布式应用——配置文件篇(转载)}

22使用.NET Remoting开发分布式应用——配置文件篇(转载)

3.客户机

配置文件:

 1使用.NET Remoting开发分布式应用——配置文件篇(转载)<?xml version="1.0" encoding="utf-8" ?>
 2使用.NET Remoting开发分布式应用——配置文件篇(转载)<configuration>
 3使用.NET Remoting开发分布式应用——配置文件篇(转载)    <system.runtime.remoting>
 4使用.NET Remoting开发分布式应用——配置文件篇(转载)        <application>
 5使用.NET Remoting开发分布式应用——配置文件篇(转载)            <client>
 6使用.NET Remoting开发分布式应用——配置文件篇(转载)                <wellknown type="RemotingConfigDemo.HelloServer, General" url="http://localhost:8086/SayHello" />
 7使用.NET Remoting开发分布式应用——配置文件篇(转载)            </client>
 8使用.NET Remoting开发分布式应用——配置文件篇(转载)            <channels>
 9使用.NET Remoting开发分布式应用——配置文件篇(转载)                <channel ref="http" port="0"></channel>
10使用.NET Remoting开发分布式应用——配置文件篇(转载)            </channels>
11使用.NET Remoting开发分布式应用——配置文件篇(转载)        </application>
12使用.NET Remoting开发分布式应用——配置文件篇(转载)    </system.runtime.remoting>
13使用.NET Remoting开发分布式应用——配置文件篇(转载)</configuration>
14使用.NET Remoting开发分布式应用——配置文件篇(转载)

客户机代码:

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

22使用.NET Remoting开发分布式应用——配置文件篇(转载)
23使用.NET Remoting开发分布式应用——配置文件篇(转载)            Console.WriteLine(
24使用.NET Remoting开发分布式应用——配置文件篇(转载)                "Client2 HTTP HelloMethod {0}",
25使用.NET Remoting开发分布式应用——配置文件篇(转载)                obj2.HelloMethod("Caveman2"));
26使用.NET Remoting开发分布式应用——配置文件篇(转载)            Console.ReadLine();
27使用.NET Remoting开发分布式应用——配置文件篇(转载)        }

28使用.NET Remoting开发分布式应用——配置文件篇(转载)    }

29使用.NET Remoting开发分布式应用——配置文件篇(转载)}

30使用.NET Remoting开发分布式应用——配置文件篇(转载)

三.需要注意的几点

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

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

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

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

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

使用.NET Remoting开发分布式应用——配置文件篇(转载)copy "$(ProjectDir)\*.config" "$(TargetDir)"

如图:
使用.NET Remoting开发分布式应用——配置文件篇(转载)
6
.在编码中,可以不要把配置文件名硬编码写死,用如下语句来代替,这是一个很好的编程实践,也是值得推荐的一种写法。

使用.NET Remoting开发分布式应用——配置文件篇(转载)AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

7.最后一点,也是最重要的一点,推荐在项目中使用配置文件!

转载于:https://www.cnblogs.com/daitengfei/archive/2006/04/04/366624.html