Freigeben über


Remoting Example: Lifetimes

The following example demonstrates several of the lifetime lease scenarios. CAOClient.exe registers a sponsor that (after the initial lease time) renews the lease at a different time from that specified in the remote type. Note that the MyClientSponsor extends MarshalByRefObject to be passed by reference to the remote lease manager; if it did not, but instead was decorated with the SerializableAttribute attribute, the sponsor would be passed by value and executed normally, but in the server application domain.

This application runs on a single computer or across a network. If you want to run this application over a network, you must replace "localhost" in the client configuration with the name of the remote computer.

This sample uses code written in both Visual Basic and C#. RemoteType.cs and CAOClient.cs are provided for documentation purposes, but are not compiled by the command lines given.

CAUTION   .NET remoting does not do authentication or encryption by default. Therefore, it is recommended that you take all necessary steps to make certain of the identity of clients or servers before interacting with them remotely. Because .NET remoting applications require FullTrust permissions to execute, if a unauthorized client were granted access on your server, the client could execute code as though it were fully trusted. Always authenticate your endpoints and encrypt the communication streams, either by hosting your remoted types in Internet Information Services (IIS) or by building a custom channel sink pair to do this work.

To compile this sample

  1. Type the following commands at the command prompt:

    vbc /t:library RemoteType.vb

    csc /r:RemoteType.dll server.cs

    vbc /r:RemoteType.dll CAOClientVB.vb

CAOClient file

//CAOClient.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("CAOclient.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);
    }
}

' CAOClientVB.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Lifetime

Public Class Client
   <MTAThread()> _
   Public Shared Sub Main()
   
      ' Loads the configuration file.
      RemotingConfiguration.Configure("CAOclient.exe.config")   
       
      Dim CAObject As ClientActivatedType = New ClientActivatedType()

      Dim ServerLease As ILease = CType(RemotingServices.GetLifetimeService(CAObject), ILease)
      Dim sponsor As MyClientSponsor = 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()

   End Sub 'Main
End Class 'Client


Public Class MyClientSponsor 
   Inherits MarshalByRefObject
   Implements ISponsor

   Private LastRenewal As DateTime

   Public Sub New()
      LastRenewal = DateTime.Now
   End Sub   ' MyClientSponsor

   Public Function Renewal(ByVal lease As ILease) As TimeSpan Implements ISponsor.Renewal
    
      Console.WriteLine("I've been asked to renew the lease.")
      Dim Latest As DateTime = DateTime.Now
      Console.WriteLine("Time since last renewal: " & (Latest.Subtract(LastRenewal)).ToString())
      LastRenewal = Latest
      Return TimeSpan.FromSeconds(20)

   End Function 'Renewal

End Class 'MyClientSponsor

RemoteType file

'RemoteType.vb
Imports System
Imports System.Runtime.Remoting.Lifetime
Imports System.Security.Principal

Public class ClientActivatedType
   Inherits MarshalByRefObject

   Public Function RemoteMethod() As String
      ' 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
   End Function  'RemoteMethod

   ' Overrides the lease settings for this object.
   Public Overrides Function InitializeLifetimeService() As Object

      Dim lease As ILease = CType(MyBase.InitializeLifetimeService(), ILease)
      
      If lease.CurrentState = LeaseState.Initial Then
         ' Normally, the initial lease time would be much longer.
         ' It is shortened here for demonstration purposes.
         lease.InitialLeaseTime = TimeSpan.FromSeconds(3)
         lease.SponsorshipTimeout = TimeSpan.FromSeconds(10)
         lease.RenewOnCallTime = TimeSpan.FromSeconds(2)
      End If

      Return lease
   End Function  'InitializeLifetimeService

End Class   'ClientActivatedType

// RemoteType.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.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();

   }
}

Server.exe.config

<configuration>
   <system.runtime.remoting>
      <application>
         <service>
            <activated type="ClientActivatedType, RemoteType"/>
         </service>
         <channels>
            <channel port="8080" ref="http"/>
         </channels>
      </application>
   </system.runtime.remoting>
</configuration>

CAOclient.exe.config

<configuration>
   <system.runtime.remoting>
      <application>
         <client url="https://localhost:8080">
            <activated type="ClientActivatedType, RemoteType"/>
         </client>
         <channels>
            <channel ref="http" port="0">
               <serverProviders>            
                  <formatter ref="soap" typeFilterLevel="Full"/>
                  <formatter ref="binary" typeFilterLevel="Full"/>
               </serverProviders>
            </channel>
         </channels>      </application>
   </system.runtime.remoting>
</configuration>

See Also

Remoting Examples | Object Activation and Lifetimes