首先,看一下SAOs配置文件:
1
<?xml version="1.0" encoding="utf-8" ?>
2
3
<configuration>
4
<system.runtime.remoting><!--所有关于Remoting的配置,可据此在MSDN中查到Remoting的所有配置节点-->
5
<application>
6
<service><!--注册一个服务-->
7
<wellknown mode="Singleton" objectUrl="SsyHello" type="General.HelloServer,General"/><!--注册一个众所周知的对象,及其它各种信息-->
8
</service>
9
<channels><!--注册通道 -->
10
<channel port="8086" ref="http"/><!--ref指引用machine.config中的配置-->
11
</channels>
12
</application>
13
</system.runtime.remoting>
14
</configuration>
<?xml version="1.0" encoding="utf-8" ?>2

3
<configuration>4
<system.runtime.remoting><!--所有关于Remoting的配置,可据此在MSDN中查到Remoting的所有配置节点-->5
<application>6
<service><!--注册一个服务-->7
<wellknown mode="Singleton" objectUrl="SsyHello" type="General.HelloServer,General"/><!--注册一个众所周知的对象,及其它各种信息-->8
</service>9
<channels><!--注册通道 -->10
<channel port="8086" ref="http"/><!--ref指引用machine.config中的配置-->11
</channels>12
</application>13
</system.runtime.remoting>14
</configuration>此时在对于Demo1中的Server端代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Runtime.Remoting.Channels;
5
using System.Runtime.Remoting;
6
using System.Runtime.Remoting.Channels.Tcp;
7
using System.Runtime.Remoting.Channels.Http;
8
using General;
9
namespace Server
10
{
11
public class Server
12
{
13
/// <summary>
14
/// 第二步,创建服务器端
15
/// </summary>
16
/// <param name="args"></param>
17
/// <returns></returns>
18
static int Main(string[] args)
19
{
20
////Remoting内置的两种通道,也可自定义自己的通道
21
//TcpChannel tcpChannel = new TcpChannel(8085);//tcp通道
22
//HttpChannel httpChannel = new HttpChannel(8086);//http通道
23
24
25
////服务器端对通道进行注册,注册完后,服务器就对通道进行监听
26
//ChannelServices.RegisterChannel(tcpChannel,false);
27
//ChannelServices.RegisterChannel(httpChannel,false);
28
29
////把远程对象注册到服务器上,注册众所周知的服务类型,WellKnown---都是服务器激活的对象
30
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer),"SayHello",WellKnownObjectMode.Singleton);
31
///*
32
// * Singleton 服务器端只产生单例,任何请求共用,保存状态,
33
// * Singlecall 客户端一个请求对应一个实例,没有状态
34
// *
35
// *
36
// *
37
// *
38
//*/
39
string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//得到APP.confg文件在程序可执行目录下生成的server.exe.config文件的路径
40
RemotingConfiguration.Configure(path);
41
Console.Write("服务器开始监听

,按任意銉退出");
42
Console.ReadLine();
43
return 0;
44
}
45
}
46
}
47
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Runtime.Remoting.Channels;5
using System.Runtime.Remoting;6
using System.Runtime.Remoting.Channels.Tcp;7
using System.Runtime.Remoting.Channels.Http;8
using General;9
namespace Server10
{11
public class Server12
{13
/// <summary>14
/// 第二步,创建服务器端15
/// </summary>16
/// <param name="args"></param>17
/// <returns></returns>18
static int Main(string[] args)19
{20
////Remoting内置的两种通道,也可自定义自己的通道21
//TcpChannel tcpChannel = new TcpChannel(8085);//tcp通道22
//HttpChannel httpChannel = new HttpChannel(8086);//http通道23

24

25
////服务器端对通道进行注册,注册完后,服务器就对通道进行监听26
//ChannelServices.RegisterChannel(tcpChannel,false);27
//ChannelServices.RegisterChannel(httpChannel,false);28

29
////把远程对象注册到服务器上,注册众所周知的服务类型,WellKnown---都是服务器激活的对象30
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer),"SayHello",WellKnownObjectMode.Singleton);31
///*32
// * Singleton 服务器端只产生单例,任何请求共用,保存状态,33
// * Singlecall 客户端一个请求对应一个实例,没有状态34
// * 35
// * 36
// * 37
// * 38
//*/39
string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//得到APP.confg文件在程序可执行目录下生成的server.exe.config文件的路径40
RemotingConfiguration.Configure(path);41
Console.Write("服务器开始监听

,按任意銉退出");42
Console.ReadLine();43
return 0;44
}45
}46
}47

从以上可以看出,通道的定义,注册及对象在服务器上的注册等环节均用配置文件实现了,同理对于客户端如下:
配置文件:
1
<?xml version="1.0" encoding="utf-8" ?>
2
<configuration>
3
<system.runtime.remoting>
4
<system.runtime.remoting>
5
<application>
6
<client>
7
<wellknown url="http://localhost:8086/SayHello" type="General.HelloServer,General"/>
8
</client>
9
<channels>
10
<channel port="0" ref="http"/>
11
</channels>
12
</application>
13
</system.runtime.remoting>
14
</system.runtime.remoting>
15
</configuration>
<?xml version="1.0" encoding="utf-8" ?>2
<configuration>3
<system.runtime.remoting>4
<system.runtime.remoting>5
<application>6
<client>7
<wellknown url="http://localhost:8086/SayHello" type="General.HelloServer,General"/>8
</client>9
<channels>10
<channel port="0" ref="http"/>11
</channels>12
</application>13
</system.runtime.remoting>14
</system.runtime.remoting>15
</configuration>客户端代码为:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using General;
namespace Client
{
class Client
{
/// <summary>
/// 第三步,创建客户端
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
////使用TCP通道得到远程对象代理,不用监听
//TcpChannel tcpChannel1 = new TcpChannel();
//ChannelServices.RegisterChannel(tcpChannel1,false);
//HelloServer helloServer1 = (HelloServer)Activator.GetObject(typeof(General.HelloServer), "tcp://localhost:8085/SayHello");
//if (helloServer1 == null)
//{
// Console.Write("Could not locate TCP server");
//}

////使用HTTP通道得到远程对象代理,不用监听
//HttpChannel httpChannel2 = new HttpChannel();
//ChannelServices.RegisterChannel(httpChannel2, false);
//HelloServer helloServer2 = (HelloServer)Activator.GetObject(typeof(General.HelloServer), "http://localhost:8086/SayHello");
//if (helloServer2 == null)
//{
// Console.Write("Could not locate HTTP server");
//}
RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
HelloServer helloServer2 = new HelloServer();
//一般方法调用
//Console.WriteLine("TCP HelloMethod {0}", helloServer1.HelloMethod("TcpChannel"));
Console.WriteLine("HTTP HelloMethod {0}", helloServer2.HelloMethod("HTTPChannel"));
//传递自定义类型参数
// Console.WriteLine("TCP HelloMethod {0}", helloServer1.HelloUserMethod(new User("wumaosheng",false)));
Console.WriteLine("http HelloMethod {0}", helloServer2.HelloUserMethod(new User("heqichang", true)));

//测试SingleTon和SingleCall,多次调用
int count;
//Console.WriteLine("TCP HelloMethod {0} count:{1}", helloServer1.HellTonOrCall("mswu",out count),count);
Console.WriteLine("http HelloMethod {0} count:{1}", helloServer2.HellTonOrCall("mswu", out count), count);
Console.WriteLine("http HelloMethod {0} count:{1}", helloServer2.HellTonOrCall("mswu", out count), count);
Console.ReadLine();
}
}
}
远程对象及其它部分均不需改变,未完,待续....
