今日学习 (使用.NET Remoting 建立分布式应用程序(二))(4月7日) ——对象生存周期管理

讲师:任旻

本次课程内容包括
lSAOs的配置文件
Ø配置文件
ØMachine.config中的通道
l租约
lCAOs

SAOs的配置文件
Ø配置文件
ØMachine.config中的通道

配置文件
l使用配置文件的好处
Ø简化代码
Ø随时更改,通道,端口,URL的设置而不用重
新编译
l.Net提供了Remoting配置文件的标准
lXML格式的配置文件
l推荐在实际项目中使用配置文件

配置文件举例
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="Singleton" objectUri="SayHello"
type="RemotingSamples.HelloServer, General" />
</service>
<channels>
<channel port="8086" ref="http"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>

配置文件举例——客户端
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown url="http://localhost:8086/SayHello"
type="RemotingSamples.HelloServer, General" />
</client >
<channels>
<channel port=“0" ref="http"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>

使用配置文件的代码
配置文件可以写在任意的.config文件中。
Server.cs
RemotingConfiguration.Configure("Server.exe.config");
Client.cs
RemotingConfiguration.Configure(@"client.exe.config");
HelloServer obj2 = new HelloServer();

租约
Ø对象的生存周期
Ø什么是租约
Ø续约

对象生存周期
l客户机检测服务器是否可用
Ø调用远程对象的方法会出现
System.Runtime.Remoting.RemotingException
l服务器检测客户机是否可用
Ø租约分布式垃圾回收器(LDGC)
Ø只对Singleton对象和客户端激活对象有效

续约
隐式续约
Ø每次调用远程对象上的方法的时候自动进行。
显示的续约
ØILease.Renew()
发起租约
ØISponsor接口
ØILease.Register()

CAOs
Ø保存客户状态
Ø客户端发起租约

客户端激活对象
服务器为每个客户端创建一个实例
在租约时间到期并且垃圾回收器发挥作用
之前对象将一直处于激活状态

客户端发起租约
发起者(Sponsor)
发起者是可以为远程对象更新租约的对
象。
ISponsor接口
ClientSponsor类
ØSystem.Runtime.Remoting.Lifetime

示例代码:

Client         Program.cs

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

public class Client
{

    public static void Main(string[] Args)
    {

        // Loads the configuration file.
        RemotingConfiguration.Configure("client.exe.config");

        ClientActivatedType CAObject = new ClientActivatedType();

        ILease serverLease = (ILease)RemotingServices.GetLifetimeService(CAObject);
        MyClientSponsor sponsor = new MyClientSponsor();

        // Note: If you do not pass an initial time, the first request will
        // be taken from the LeaseTime settings specified in the
        // server.exe.config file.
        serverLease.Register(sponsor);

        // Calls same method on each object.

        Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod());

        Console.WriteLine("Press Enter to end the client application domain.");
        Console.ReadLine();
    }
}


public class MyClientSponsor : MarshalByRefObject, ISponsor
{

    private DateTime lastRenewal;

    public MyClientSponsor()
    {
        lastRenewal = DateTime.Now;
    }

    public TimeSpan Renewal(ILease lease)
    {

        Console.WriteLine("I've been asked to renew the lease.");
        Console.WriteLine("Time since last renewal:" + (DateTime.Now - lastRenewal).ToString());

        lastRenewal = DateTime.Now;
        return TimeSpan.FromSeconds(20);
    }
}

 

General   ClientActivatedType.cs

using System;
using System.Runtime.Remoting.Lifetime;
using System.Security.Principal;

public class ClientActivatedType : MarshalByRefObject
{


    // Overrides the lease settings for this object.
    public override Object InitializeLifetimeService()
    {

        ILease lease = (ILease)base.InitializeLifetimeService();
        // Normally, the initial lease time would be much longer.
        // It is shortened here for demonstration purposes.
        if (lease.CurrentState == LeaseState.Initial)
        {
            lease.InitialLeaseTime = TimeSpan.FromSeconds(3);
            lease.SponsorshipTimeout = TimeSpan.FromSeconds(10);
            lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
        }
        return lease;
    }

    public string RemoteMethod()
    {

        // Announces to the server that the method has been called.
        Console.WriteLine("ClientActivatedType.RemoteMethod called.");

        // Reports the client identity name.
        return "RemoteMethod called. " + WindowsIdentity.GetCurrent().Name;

    }
}

 

Server    Program.cs

using System;
using System.Runtime.Remoting;


public class Server
{

    public static void Main(string[] Args)
    {

        // Loads the configuration file.
        RemotingConfiguration.Configure("server.exe.config");

        Console.WriteLine("The server is listening. Press Enter to exit....");
        Console.ReadLine();

        Console.WriteLine("Recycling memory...");
        GC.Collect();
        GC.WaitForPendingFinalizers();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值