默认对象的租赁期是5分钟,但我们可以自己配置,也可通过程序设定租赁时间
1.配置实现(它对这个应用程序中的所有对象都适用)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<lifetime leaseTime="10s" renewOnCallTime="5s"/>
<!--Config Server and channels as before-->
</application>
</system.runtime.remoting>
</configuration>2.程序实现
程序实现的好处在于可以为每个类型都定义适合自己的租赁租,必须为每个类型写入代码,也可以通过继承于MarshByRefObject类的一个方法InitializeLifetimeServices来设置.运行库会在创建对象时调用这个方法.
通过如下类查看对象租赁期情况:
服务器端程序:
客户端程序:
后记:
如果想设置无限的租赁,则在InitializeLifetimeService()中返回null对象来实现:
1.配置实现(它对这个应用程序中的所有对象都适用)









程序实现的好处在于可以为每个类型都定义适合自己的租赁租,必须为每个类型写入代码,也可以通过继承于MarshByRefObject类的一个方法InitializeLifetimeServices来设置.运行库会在创建对象时调用这个方法.
1
using System;
2
using System.Runtime.Remoting;
3
using System.Runtime.Remoting.Lifetime;
4![]()
5
namespace CheckLife
6
{
7
/// <summary>
8
/// Customer 的摘要说明。
9
/// </summary>
10
public class Customer:MarshalByRefObject
11
{
12
string mName;
13
public Customer(string name)
14
{
15
Console.WriteLine("Customer.ctor({0})",name);
16
mName = name;
17
}
18
public Customer()
19
{
20
//
21
// TODO: 在此处添加构造函数逻辑
22
//
23
}
24
public override object InitializeLifetimeService()
25
{
26
ILease leaseInfo = (ILease)base.InitializeLifetimeService();
27
leaseInfo.InitialLeaseTime = TimeSpan.FromSeconds(7);
28
leaseInfo.RenewOnCallTime = TimeSpan.FromSeconds(3);
29
return leaseInfo;
30
}
31![]()
32
public string SayHello()
33
{
34
Console.WriteLine("Customemr.SayHello()");
35
Diagnostics.ShowLeaseInfo((ILease)this.GetLifetimeService());
36
return "Hello"+mName;
37
}
38
}
39
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

1
using System;
2
using System.Runtime;
3
using System.Runtime.Remoting;
4
using System.Runtime.Remoting.Lifetime;
5
namespace CheckLife
6
{
7
/// <summary>
8
/// Diagnostics 的摘要说明。
9
/// </summary>
10
public class Diagnostics
11
{
12
public static void ShowLeaseInfo(ILease leaseInfo)
13
{
14
Console.WriteLine("Lease Info![]()
![]()
.");
15
if(leaseInfo != null)
16
{
17
Console.WriteLine("Current Lease time:{0}:{1}",leaseInfo.CurrentLeaseTime.Minutes,leaseInfo.CurrentLeaseTime.Seconds);
18
Console.WriteLine("Initial Lease Time:{0}:{1}",leaseInfo.InitialLeaseTime.Minutes,leaseInfo.InitialLeaseTime.Seconds);
19
Console.WriteLine("Renew on call Time:{0}:{1}",leaseInfo.RenewOnCallTime.Minutes,leaseInfo.RenewOnCallTime.Seconds);
20
Console.WriteLine("Current state:{0}",leaseInfo.CurrentState);
21
}
22
else
23
{
24
Console.WriteLine("No Lease Info!!");
25
}
26
}
27
}
28
}

2

3

4

5

6

7

8

9

10

11

12

13

14




15

16

17

18

19

20

21

22

23

24

25

26

27

28

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25






26

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![]()
6
namespace CheckLife
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(Customer),"http://localhost:13101");
22
Customer cust = new Customer("Homer");
23
Console.WriteLine(cust.SayHello());
24
}
25
}
26
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

如果想设置无限的租赁,则在InitializeLifetimeService()中返回null对象来实现:
1
public override object InitializeLifetimeService()
2
{
3
return null;
4
}

2

3

4
