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

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

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

 

一.配置文件服务器

1.服务器配置文件:app

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

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

 

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

全部的远程配置项必须做为子元素添加到<system.runtime.remoting>下面。url

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

应用程序所提供的服务必须做为<service>的子元素列出,这就是远程对象自己,可使用<wellknown>元素来指定远程对象,mode属性能够指定为SingleCallSingleton,在后面咱们会说到。同时用type属性来指定已经定义了类型的对象,只须要指定程序集的名称便可,不须要扩展名DLLcode

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

2.客户机配置文件:

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

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

 

同服务器配置文件的元素同样,不一样的是此次是客户机通道,因此它不须要指定端口号,咱们能够暂时指定为0号。其余的保持不变。

 

二.示例程序

1.远程对象代码:

using System;
using System.Text;
using System.Runtime.Remoting.Lifetime;

namespace RemotingConfigDemo
{
    public class HelloServer : MarshalByRefObject
    {
        public HelloServer()
        {
            Console.WriteLine("服务器激活……");
        }
        public String HelloMethod(String name)
        {
            Console.WriteLine(
                "服务器端 :{0}", name);
            return "这里是:" + name;
        }
 

    }
}

 

2.服务器

配置文件:

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

 

服务器代码:

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

namespace RemotingConfigDemo 
{

    public class Server
    {
        public static int Main(string [] args) 
        {
            RemotingConfiguration.Configure("Server.exe.config");

            System.Console.WriteLine("按任意键退出……");
            System.Console.ReadLine();
            return 0;
        }
    }
}

 

3.客户机

配置文件:

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

 

客户机代码:

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

namespace RemotingConfigDemo 
{
    public class Client
    {
        public static void Main(string[] args)
        {
            //使用HTTP通道获得远程对象
            RemotingConfiguration.Configure("Client.exe.config");
            HelloServer obj2 = new HelloServer();
            if (obj2 == null)
            {
                System.Console.WriteLine(
                    "链接HTTP服务器失败……");
            }

            Console.WriteLine(
                "Client2 HTTP HelloMethod {0}",
                obj2.HelloMethod("Caveman2"));
            Console.ReadLine();
        }
    }
}

 

 

三.须要注意的几点

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

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

3.通常来讲,咱们可让应用程序的配置文件名和可执行文件的文件名相同,其后跟有文件扩展名.config

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

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

 

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

 

 

如图:

6
.在编码中,能够不要把配置文件名硬编码写死,用以下语句来代替,这是一个很好的编程实践,也是值得推荐的一种写法。

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

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

出处:http://www.cnblogs.com/Terrylee/archive/2005/11/17/278366.html

相关文章
相关标签/搜索