在例子一中,会存在3个问题:
1.创建通道,对象注册和其他配置代码过多,如果这些细节改变,则需要重新编译
2.对于已知(Well-Known)对象,只能通过默认的构造函数来创建对象,客户端不能指定非默认的构造函数
3.客户端必须引用包含已知类型的程序集,因此需要把这个程序集部署到每个客户端上,违背了分布式的原则
本节将解决问题2
首先定义一个简单的类Customer,实现了一个非默认的构造函数和一个SayHello方法
服务器端代码:
注意用到RemotingConfiguration.RegisterActivatedServiceType()方法注册服务
客户端代码:
执行结果如下:
服务器端执行情况:
![]()
客户端情况:
1.创建通道,对象注册和其他配置代码过多,如果这些细节改变,则需要重新编译
2.对于已知(Well-Known)对象,只能通过默认的构造函数来创建对象,客户端不能指定非默认的构造函数
3.客户端必须引用包含已知类型的程序集,因此需要把这个程序集部署到每个客户端上,违背了分布式的原则
本节将解决问题2
首先定义一个简单的类Customer,实现了一个非默认的构造函数和一个SayHello方法
1
using System;
2![]()
3
namespace MathLibrary
4
{
5
/// <summary>
6
/// Customer 的摘要说明。
7
/// </summary>
8
public class Customer:MarshalByRefObject
9
{
10
string mName;
11
//注意,是带参数的构造函数
12
public Customer(string name)
13
{
14
//
15
// TODO: 在此处添加构造函数逻辑
16
//
17
Console.WriteLine("Customer.ctor({0}",name);
18
mName = name;
19
}
20
public string SayHello()
21
{
22
Console.WriteLine("Customer.SayHello()");
23
return "Hello"+mName;
24
}
25
}
26
}
using System;2

3
namespace MathLibrary4
{5
/// <summary>6
/// Customer 的摘要说明。7
/// </summary>8
public class Customer:MarshalByRefObject9
{10
string mName;11
//注意,是带参数的构造函数12
public Customer(string name)13
{14
//15
// TODO: 在此处添加构造函数逻辑16
//17
Console.WriteLine("Customer.ctor({0}",name);18
mName = name;19
}20
public string SayHello()21
{22
Console.WriteLine("Customer.SayHello()");23
return "Hello"+mName;24
}25
}26
}注意用到RemotingConfiguration.RegisterActivatedServiceType()方法注册服务
1
using System;
2
using System.Runtime.Remoting;
3
using System.Runtime.Remoting.Channels;
4
using System.Runtime.Remoting.Channels.Http;
5
using MathLibrary;
6
namespace MathServer
7
{
8
/// <summary>
9
/// ServerMain 的摘要说明。
10
/// </summary>
11
public class ServerMain
12
{
13
public ServerMain()
14
{
15
//
16
// TODO: 在此处添加构造函数逻辑
17
//
18
}
19
public static void Main(string[] args)
20
{
21
HttpChannel channel = new HttpChannel(13101);
22
ChannelServices.RegisterChannel(channel);
23
RemotingConfiguration.RegisterActivatedServiceType(typeof(MathLibrary.Customer));
24![]()
25
Console.WriteLine("ServerStarted.Press Enter to End![]()
![]()
.");
26
Console.ReadLine();
27
}
28
}
29
}
using System;2
using System.Runtime.Remoting;3
using System.Runtime.Remoting.Channels;4
using System.Runtime.Remoting.Channels.Http;5
using MathLibrary;6
namespace MathServer7
{8
/// <summary>9
/// ServerMain 的摘要说明。10
/// </summary>11
public class ServerMain12
{13
public ServerMain()14
{15
//16
// TODO: 在此处添加构造函数逻辑17
//18
}19
public static void Main(string[] args)20
{21
HttpChannel channel = new HttpChannel(13101);22
ChannelServices.RegisterChannel(channel);23
RemotingConfiguration.RegisterActivatedServiceType(typeof(MathLibrary.Customer));24

25
Console.WriteLine("ServerStarted.Press Enter to End

.");26
Console.ReadLine();27
}28
}29
} 1
using System;
2
using System.Runtime.Remoting;
3
using System.Runtime.Remoting.Channels;
4
using System.Runtime.Remoting.Channels.Http;
5
using MathLibrary;
6
namespace MathClient
7
{
8
/// <summary>
9
/// ClientMain 的摘要说明。
10
/// </summary>
11
public class ClientMain
12
{
13
public ClientMain()
14
{
15
//
16
// TODO: 在此处添加构造函数逻辑
17
//
18
}
19
public static void Main(string[] args)
20
{
21
RemotingConfiguration.RegisterActivatedClientType(typeof(MathLibrary.Customer),"http://localhost:13101");
22![]()
23
Customer cust = new Customer("Homer");
24
Console.WriteLine(cust.SayHello());
25
Console.ReadLine();
26
}
27
}
28
}
using System;2
using System.Runtime.Remoting;3
using System.Runtime.Remoting.Channels;4
using System.Runtime.Remoting.Channels.Http;5
using MathLibrary;6
namespace MathClient7
{8
/// <summary>9
/// ClientMain 的摘要说明。10
/// </summary>11
public class ClientMain12
{13
public ClientMain()14
{15
//16
// TODO: 在此处添加构造函数逻辑17
//18
}19
public static void Main(string[] args)20
{21
RemotingConfiguration.RegisterActivatedClientType(typeof(MathLibrary.Customer),"http://localhost:13101");22

23
Customer cust = new Customer("Homer");24
Console.WriteLine(cust.SayHello());25
Console.ReadLine();26
}27
}28
}服务器端执行情况:

客户端情况:

浙公网安备 33010602011771号