RemotingServices::GetLifetimeService Method
Returns a lifetime service object that controls the lifetime policy of the specified object.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- obj
- Type: System::MarshalByRefObject
The object to obtain lifetime service for.
| Exception | Condition |
|---|---|
| SecurityException | The immediate caller does not have infrastructure permission. |
For the default lifetime service the returned object will be an object of type ILease. If the obj parameter is nullptr, the method returns nullptr.
The following code example demonstrates how to use the GetLifetimeService method to get a lifetime lease for the specified object.
#using <system.dll> #using <system.runtime.remoting.dll> #using "timerservice.dll" using namespace System; using namespace System::Net::Sockets; using namespace System::Runtime::Remoting; using namespace System::Runtime::Remoting::Channels; using namespace System::Runtime::Remoting::Channels::Http; using namespace System::Runtime::Remoting::Messaging; using namespace System::Runtime::Remoting::Lifetime; using namespace TimerSample; using namespace System::Security::Permissions; namespace GroupCoffeeTimer { public ref class TimerClient: public MarshalByRefObject, public ISponsor { public: [SecurityPermissionAttribute(SecurityAction::Demand, Flags=SecurityPermissionFlag::Infrastructure)] TimerClient() { // Registers the HTTP Channel so that this client can receive // events from the remote service. ChannelServices::RegisterChannel( gcnew HttpChannel( 0 ), false ); WellKnownClientTypeEntry^ remoteType = gcnew WellKnownClientTypeEntry( TimerService::typeid,"http://localhost:9000/MyService/TimerService.soap" ); RemotingConfiguration::RegisterWellKnownClientType( remoteType ); TimerService^ groupTimer = gcnew TimerService; groupTimer->MinutesToTime = 4.0; // Registers this client as a lease sponsor so that it can // prevent the expiration of the TimerService. ILease^ leaseObject = dynamic_cast<ILease^>(RemotingServices::GetLifetimeService( groupTimer )); leaseObject->Register( this ); // Subscribes to the event so that the client can receive notifications from the server. groupTimer->TimerExpired += gcnew TimerExpiredEventHandler( this, &TimerClient::OnTimerExpired ); Console::WriteLine( "Connected to TimerExpired event" ); groupTimer->Start(); Console::WriteLine( "Timer started for {0} minutes.", groupTimer->MinutesToTime ); Console::WriteLine( "Press enter to end the client process." ); Console::ReadLine(); } private: void OnTimerExpired( Object^, TimerServiceEventArgs^ e ) { Console::WriteLine( "TimerHelper::OnTimerExpired: {0}", e->Message ); } public: [System::Security::Permissions::PermissionSet(System::Security:: Permissions::SecurityAction::Demand, Name = "FullTrust")] virtual TimeSpan Renewal( ILease^ ) { Console::WriteLine( "TimerClient: Renewal called." ); return TimeSpan::FromMinutes( 0.5 ); } }; } int main() { using namespace GroupCoffeeTimer; gcnew TimerClient; }
To compile and run this example, you will need to compile and run a server, timerserver.exe, and compile a shared library, timerservice.dll.
The source for timerserver.exe follows:
The source for timerservice.dll follows:
- SecurityPermission
for operating with infrastructure code. Demand value: SecurityAction::LinkDemand; Permission value: SecurityPermissionFlag::Infrastructure
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.