[Repost] Microsoft .NET Remoting [Advanced]: Marshal, Disconnect and Life Cycle and Tracking Services

zhaozj2021-02-16  121

Preface: The so-called advanced articles here is for yourself, and it is also corresponding to the previous article "Microsoft .NET Remoting [Basics]". Maybe I want to describe is actually very simple, and it is also very basic knowledge. Because remoting's content, I don't know if I have my own ability to write a series?

First, the activation of the remote object

There are three active methods in Remoting, and the general implementation is completed by the REMOTINGSERVICES class. The work process is in fact to register the remote object into the channel. Since Remoting does not provide the corresponding unregister method to log out of the remote object, if you need to register / log out of the specified object, Microsoft recommends using Marshal (generally translated into group) and Disconnect pairing. In "Microsoft .NET Remoting [Basics]" I have already disclosed that the Marshal () method is to convert the MarshalByrefObject class object to the Objref class object, which is a storage generated agent to communicate with the remote object communication. This allows the instance to be serialized in order to transfer between the application domain and over the network, and the client can call. The disconnect () method will open the specific instance object from the channel.

According to the above description, the MARSHAL () method groups the remote object in a reference method (Mrshal-by-Reference, MBR) and places the agent's proxy information in the channel. The client can be obtained by activator.getObject (). If the user wants to cancel the object, the disconnect () method is called. So do you have the management of the lifecycle to the management cycle for the process of grouping. This is the problem to be described herein.

Second, life cycle

In the CLR, the frame provides a GC (garbage collector) to manage the life cycle of the object in memory. Similarly, .NET Remoting uses a distributed garbage collection, lease-based forms to manage the life cycle of the remote object.

Early DCOM's management of the object lifecycle is to determine when the object is used as garbage through the ping and reference count. However, the network traffic caused by ping is a painful burden on the performance of distributed applications, which greatly affects the overall performance of distributed processing. .NET Remoting In each application domain, a lease manager is introduced, saving a reference to the rental object for each server-side Singleton, or a remote object that per client activation. (Description: For the server-side SINGLECALL mode, because it is stateless, for each activated remote object, it is reclaimed by the CLR GC, so there is no lifecycle management for the remote object of SingleCall mode. .)

1, rent

Rent is an object that encapsulates the Timespan value to manage the survival of the remote object. The ILASE interface for defining the rental feature is provided in .NET Remoting. When Remoting activates a remote object via a Singleton mode or a client activation mode, the rental object calls the INITIALIZELIFETIMESERVICE method inherited from System.MarshalByRefObject, requests to rent to the object.

The ILAASE interface defines the properties of the lifecycle, all of which are Timespan values. As follows: InitialLeaseTime: Initialization effective time, the default value is 300 seconds, if 0, indicates never expired; RenewonCallTime: Call the rents update time when calling the remote object, the default is 120 seconds; sponsorshipTimeout: Timeout value, notify sponsor Promoters) After renting, Remoting will wait for time, the default is 120 seconds; Currentleasetime: The current lease time, for the first time, for the value of InitializEleasetime. Remoting's remote objects have inherited the MarshalByrefObject, so the initializelifetimeService method is inherited by default, then the relevant properties of the lease are default. If you want to change these settings, you can rewrite the method in the remote object. E.g:

public override object InitializeLifetimeService () {ILease lease = (ILease) base.InitializeLifetimeService (); if (lease.CurrentState == LeaseState.Initial) {lease.InitialLeaseTime = TimeSpan.FromMinutes (1); lease.RenewOnCallTime = TimeSpan.FromSeconds ( 20);} Return Lease;

This method can also be ignored, and the lease cycle of the object is also changed to unlimited:

Public override Object InitializelifetimeService () {Return Null;}

2, lease manager

If it is the rental of the foregoing is mainly applied to each specific remote object, the lease manager is a manager that the server is specifically used to manage the life cycle of the remote object, which maintains a system.hashtable member and will rent a map The SYSTEM.DATETIME instance indicates when each lease should expire. Remoting uses polling to wake up the lease manager in a certain time, check if each lease is expired. The default is awake every 10 seconds. Polling intervals can be configured, such as setting polling intervals for 5 minutes:

LifetimeService.LeSemanagerPollTime = System.TimeSpan.fromminutes (5);

We can also set up properties for remote object rent in the lease manager, such as changing the initial effective time of the remote object is permanent:

LifetimeServices.Leasetime = Timespan.zero;

We can also set up life cycles through profiles, such as:

Note: PollTime in the configuration file is the polling interval of the lease manager mentioned above LeaseManagerPollTime.

Lease Manager's settings for life cycles are all remote objects on the server. When we set up the rented properties by profile or leased manager, all the life cycles of all remote objects follow this setting unless we change the relevant configuration by rewriting the INITIALIZELIFETIMESERVICE method for the specified remote object. That is, the leased configuration priority of the remote object is higher than the server side configuration.

3, sponsor (sponsor)

The initiator is for the client. Remote object is an object that sponsor to lease, and the initiator can sign a lease with the server side, agreeing to rent. Once expired, the initiator can still renew, just like the rents of the rents in real life, the relationship between landlords and rentals.

Defining the ClientSponsor class in the system.runtime.remoting.lifetime namespace in .NET Framework, the class inherits System.MarshalByrefObject and implements the ISPonsor interface. Attributes and methods of the ClientSponsor class, you can refer to MSDN.

The client should use the initiator mechanism to create an instance of the ClientSponsor class. Then call the relevant method such as the register () or renewal () method to register the remote object or extend the life cycle. Such as:

RemotingObject Obj = new remoteObject (); clientsponsor sponsor = new clientsponsor (); sponsor.renewaltime = timespan.fromminutes (2); sponsor.register (obj);

The renewal time can also be set directly in the constructor of ClientSponsor, such as:

ClientSponsor sponsor = new clientsponsor (Timespan.fromminutes (2)); sponsor.register (obj);

We can also write sponsor to manage sponsor mechanisms, which must inherit ClientSponsor and implement the ISPonsor interface.

Third, tracking service

As mentioned earlier, we have to judge whether there is a management cycle through the MARSHAL group remote object. In Remoting, you can monitor the grouping process of the MBR object by tracking the service program.

We can create a simple tracking handler that implements interface iTrackingHandler. Interface ItrackingHandler defines 3 methods, Marshalobject, Unmarshalobject, and DisconnectedObject. When the remote object is encapshed, unconnected, unconnected, the corresponding method is called. Below is the code for the tracking process class: PUBLIC CLASSMYTRACKING: ItrackingHandler {public myTracking () {// // Todo: Add constructor logic //}

Public void mathaledobject (object obj, objref or) {console.writeline (); console.writeline ("object" Obj.toString () "is mathaled at" DateTime.now.toshostTimeString ());}

Public void unmarshaledObject (object obj, objref or) {console.writeline (); console.writeline ("object" Obj.toString () "is unmarshaled at" DateTime.now.toshostTimeString ());}

Public void disconnectedObject (object obj) {console.writeline (obj.tostring () "is disconnected at" DateTime.now.toshostTimeString ());}}

Then the server side creates an instance of the tracking process class, and register the trace service:

TrackingServices.register (new mytracking ());

Fourth, test

1. Build two remote objects and rewrite the initializelifetimeservice method:

Object 1: AppService1 Initial Life Cycle: 1 minute

Public class appservice1: MarshalByrefObject {public void printstring (string contents) {console.writeLine (contents);}

public override object InitializeLifetimeService () {ILease lease = (ILease) base.InitializeLifetimeService (); if (lease.CurrentState == LeaseState.Initial) {lease.InitialLeaseTime = TimeSpan.FromMinutes (1); lease.RenewOnCallTime = TimeSpan.FromSeconds ( 20);} return lease;}}

Object 2: AppService2 Initial Life Cycle: 3 minutes

Public class appservice2: MarshalByrefObject {public void printstring (string contents) {console.writeline (content);}

public override object InitializeLifetimeService () {ILease lease = (ILease) base.InitializeLifetimeService (); if (lease.CurrentState == LeaseState.Initial) {lease.InitialLeaseTime = TimeSpan.FromMinutes (3); lease.RenewOnCallTime = TimeSpan.FromSeconds ( 40);} Return Lease;}} is the same as the simply beginning, the two object methods are the same.

2, server side

(1) First establish the above monitoring processing class;

(2) Registration channel:

TCPChannel Channel = New Tcpchannel (8080); ChannelServices.registerChannel (Channel);

(3) Set the initial rental time of the leased manager is unlimited:

LifetimeServices.Leasetime = Timespan.zero;

(4) Create an instance of the tracking process class, and register the trace service:

TrackingServices.register (new mytracking ());

(5) Group two remote objects:

Serveras.AppService1 Service1 = New Serveras1.appService1 (); Objref Objref1 = RemotingServices.Marshal ((MarshalByrefObject) Service1, "AppService1");

Serveras.AppService2 Service2 = New Serveras1.appService2 (); Objref Objref2 = RemotingServices.Marshal (MarshalByrefObject) Service2, "AppService2");

(6) Keep the server side:

Console.writeline ("Remoting Service Start, press Exit ..."); console.readline ();

3, client

Two remote objects are obtained by activator.getObject () and calls its method PrintString. The code is slightly.

4, run test:

Run the server side and the client, because the monitor will monitor the group process of the remote object, so the remote object is already Marshal:

Note: Now there is a problem now, I have no way to post, wait until normally will be toned.

Then then the client calls the printstring method of these two remote objects, the server is accepting the string:

After a minute, the remote object is automatically used by disconnect:

At this point, the client will call the remote object one, will throw the RemotInGexception;

After another minute, the second master of the remote object was disconnect:

转载请注明原文地址:https://www.9cbs.com/read-8845.html

New Post(0)