RemotingServices.GetLifetimeService メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > RemotingServices.GetLifetimeService メソッドの意味・解説 

RemotingServices.GetLifetimeService メソッド

指定したオブジェクト有効期間ポリシー制御する有効期間サービス オブジェクト返します

名前空間: System.Runtime.Remoting
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function GetLifetimeService
 ( _
    obj As MarshalByRefObject _
) As Object
Dim obj As MarshalByRefObject
Dim returnValue As Object

returnValue = RemotingServices.GetLifetimeService(obj)
public static Object GetLifetimeService (
    MarshalByRefObject obj
)
public:
static Object^ GetLifetimeService (
    MarshalByRefObject^ obj
)
public static Object GetLifetimeService (
    MarshalByRefObject obj
)
public static function GetLifetimeService
 (
    obj : MarshalByRefObject
) : Object

パラメータ

obj

有効期間サービス取得する対象となるオブジェクト

戻り値
obj有効期間制御するオブジェクト

例外例外
解説解説
使用例使用例

GetLifetimeService メソッド使用して指定したオブジェクト有効期間リース取得する方法コード例次に示します

Imports System
Imports System.Collections
Imports System.Net.Sockets
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Remoting.Lifetime
Imports System.Security.Permissions
Imports TimerService



Public Class TimerClient
    Inherits MarshalByRefObject
    Implements ISponsor
    
    Public Shared Sub Main()
 
        Dim myClient As New
 TimerClient()
    
    End Sub 'Main
    
    <SecurityPermission(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.Infrastructure)>
 _
    Public Sub New() 
        ' Registers the HTTP Channel so that this client can receive
        ' events from the remote service.
        Dim serverProv As New
 BinaryServerFormatterSinkProvider()
        serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
        Dim clientProv As New
 BinaryClientFormatterSinkProvider()
        
        Dim props As New
 Hashtable()
        props("port") = 0
        Dim channel As New
 HttpChannel(props, clientProv, serverProv)
        ChannelServices.RegisterChannel(channel)
        
        Dim remoteType As New
 WellKnownClientTypeEntry(GetType(TimerService), "http://localhost:9000/MyService/TimerService.soap")
        RemotingConfiguration.RegisterWellKnownClientType(remoteType)
        
        Dim groupTimer As New
 TimerService()
        groupTimer.MinutesToTime = 4.0
        
        ' Registers this client as a lease sponsor so that it can
        ' prevent the expiration of the TimerService.
        Dim leaseObject As ILease = CType(RemotingServices.GetLifetimeService(groupTimer),
 ILease)
        leaseObject.Register(Me)
        
        ' Subscribes to the event so that the client can receive notifications
 from the server.
        AddHandler groupTimer.TimerExpired, AddressOf
 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()
    
    End Sub 'New
    
    
    Public Sub OnTimerExpired(ByVal
 [source] As Object, ByVal
 e As TimerServiceEventArgs) 
        Console.WriteLine("TimerHelper.OnTimerExpired: {0}",
 e.Message)
    
    End Sub 'OnTimerExpired
    
    <SecurityPermission(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.Infrastructure)>
 _
    Public Function Renewal(ByVal
 lease As ILease) As TimeSpan Implements
 ISponsor.Renewal
        Console.WriteLine("TimerClient: Renewal called.")
        Return TimeSpan.FromMinutes(0.5)
    
    End Function 'Renewal
End Class 'TimerClient
using System;
using System.Collections;
using System.Net.Sockets;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;
using System.Security.Permissions;
using TimerSample;

namespace GroupCoffeeTimer {
    public class TimerClient : MarshalByRefObject,
 ISponsor {
        public static void
 Main() {
            TimerClient myClient = new TimerClient();
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        public TimerClient() {
            // Registers the HTTP Channel so that this client can receive
            // events from the remote service.

            BinaryServerFormatterSinkProvider serverProv = new
 BinaryServerFormatterSinkProvider();
            serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
            BinaryClientFormatterSinkProvider clientProv = new
 BinaryClientFormatterSinkProvider();

            IDictionary props = new Hashtable();
            props["port"] = 0;
            HttpChannel channel = new HttpChannel(props, clientProv,
 serverProv);
            ChannelServices.RegisterChannel(channel);

            WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(TimerService),
 "http://localhost:9000/MyService/TimerService.soap");
            RemotingConfiguration.RegisterWellKnownClientType(remoteType);
            
            TimerService groupTimer = new TimerService();
            groupTimer.MinutesToTime = 4.0; 

            // Registers this client as a lease sponsor so that it can
            // prevent the expiration of the TimerService.
            ILease leaseObject = (ILease)RemotingServices.GetLifetimeService(groupTimer);
            leaseObject.Register(this);
            
            // Subscribes to the event so that the client can receive
 notifications from the server.
            groupTimer.TimerExpired += new TimerExpiredEventHandler(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();
        }
        
        public void OnTimerExpired (object
 source, TimerServiceEventArgs e) {
            Console.WriteLine("TimerHelper.OnTimerExpired: {0}", e.Message);
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        public TimeSpan Renewal(ILease lease) {
            Console.WriteLine("TimerClient: Renewal called.");
            return TimeSpan.FromMinutes(0.5);
        }
    }
}
#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 ) );
         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();
      }

      void OnTimerExpired( Object^, TimerServiceEventArgs^ e )
      {
         Console::WriteLine( "TimerHelper::OnTimerExpired: {0}", e->Message
 );
      }

      [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;
}

この例をコンパイルして実行するには、サーバー (timerserver.exe) をコンパイルして実行し共有ライブラリ (timerservice.dll) をコンパイルする必要があります

timerserver.exe のソース次に示します

Imports System
Imports System.Collections
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Lifetime
Imports System.Timers



Public Class TimerServer
    
    Public Shared Sub Main()
 
        
        Dim serverProv As New
 BinaryServerFormatterSinkProvider()
        serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full
        Dim clientProv As New
 BinaryClientFormatterSinkProvider()
        
        Dim props As New
 Hashtable()
        props("port") = 9000
        Dim channel As New
 HttpChannel(props, clientProv, serverProv)
        ChannelServices.RegisterChannel(channel)
        RemotingConfiguration.RegisterWellKnownServiceType(GetType(TimerService),
 "MyService/TimerService.soap", WellKnownObjectMode.Singleton)
        
        Console.WriteLine("Press enter to end the server process.")
        Console.ReadLine()
    
    End Sub 'Main
End Class 'TimerServer

using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Lifetime;
using System.Timers;

namespace TimerSample {
    public class TimerServer {
        public static void
 Main() {

            BinaryServerFormatterSinkProvider serverProv = new
 BinaryServerFormatterSinkProvider();
            serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
            BinaryClientFormatterSinkProvider clientProv = new
 BinaryClientFormatterSinkProvider();

            IDictionary props = new Hashtable();
            props["port"] = 9000;
            HttpChannel channel = new HttpChannel(props, clientProv,
 serverProv);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(
                                        typeof(TimerService), 
                                        "MyService/TimerService.soap",
                                        WellKnownObjectMode.Singleton);
            
            Console.WriteLine("Press enter to end the server process.");
            Console.ReadLine();
        }
    }
}

timerservice.dll のソース次に示します

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Lifetime
Imports System.Security.Permissions
Imports System.Timers


' Define the event arguments
<Serializable()>  _
Public Class TimerServiceEventArgs
    Inherits EventArgs
    Private m_Message As String
    
    Public Sub New(ByVal
 message As String) 
        m_Message = message
    
    End Sub 'New
    
    
    Public ReadOnly Property
 Message() As String 
        Get
            Return m_Message
        End Get
    End Property
End Class 'TimerServiceEventArgs


' Define the delegate for the event
Public Delegate Sub TimerExpiredEventHandler(ByVal
 sender As Object, ByVal
 e As TimerServiceEventArgs) 

' Define the remote service class

Public Class TimerService
    Inherits MarshalByRefObject
    Private m_MinutesToTime As Double
    Private m_Timer As Timer
    
    ' The client will subscribe and unsubscribe to this event
    Public Event TimerExpired As
 TimerExpiredEventHandler
    
    
    ' Default: Initialize the TimerService to 4 minutes, the time required
    ' to brew coffee in a French Press.
    Public Sub New() 
        MyClass.New(4.0)
    
    End Sub 'New
    
    
    Public Sub New(ByVal
 minutes As Double) 
        Console.WriteLine("TimerService instantiated.")
        m_MinutesToTime = minutes
        m_Timer = New Timer()
        AddHandler m_Timer.Elapsed, AddressOf
 OnElapsed
    
    End Sub 'New
    
    
    Public Property MinutesToTime() As
 Double 
        Get
            Return m_MinutesToTime
        End Get
        Set
            m_MinutesToTime = value
        End Set
    End Property
    
    
    Public Sub Start() 
        If Not m_Timer.Enabled Then
            Dim interval As TimeSpan = TimeSpan.FromMinutes(m_MinutesToTime)
            m_Timer.Interval = interval.TotalMilliseconds
            m_Timer.Enabled = True
        Else
        End If
     ' TODO: Raise an exception
    End Sub 'Start
    
    
    Public Sub OnElapsed(ByVal
 [source] As Object, ByVal
 e As ElapsedEventArgs) 
        m_Timer.Enabled = False
        
        ' Fire Event
        If Not (TimerExpiredEvent Is
 Nothing) Then
            ' Package String in TimerServiceEventArgs
            Dim timerEventArgs As New
 TimerServiceEventArgs("TimerServiceEventArgs: Timer Expired.")
            Console.WriteLine("Firing TimerExpired Event")
            RaiseEvent TimerExpired(Me, timerEventArgs)
        End If
    
    End Sub 'OnElapsed
    
    <SecurityPermission(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.Infrastructure)>
 _
    Public Overrides Function
 InitializeLifetimeService() As [Object] 
        Dim lease As ILease = CType(MyBase.InitializeLifetimeService(),
 ILease)
        If lease.CurrentState = LeaseState.Initial Then
            lease.InitialLeaseTime = TimeSpan.FromMinutes(0.125)
            lease.SponsorshipTimeout = TimeSpan.FromMinutes(2)
            lease.RenewOnCallTime = TimeSpan.FromSeconds(2)
            Console.WriteLine("TimerService: InitializeLifetimeService")
        End If
        Return lease
    
    End Function 'InitializeLifetimeService
End Class 'TimerService

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Lifetime;
using System.Security.Permissions;
using System.Timers;

namespace TimerSample {
    // Define the event arguments
    [Serializable]
    public class TimerServiceEventArgs : EventArgs
 {
        private string m_Message;
        public TimerServiceEventArgs(string
 message) {
            m_Message = message;
        }

        public string Message {
            get {
                return m_Message;
            }
        }
    }
    
    // Define the delegate for the event
    public delegate void TimerExpiredEventHandler
 (object sender, TimerServiceEventArgs e);

    // Define the remote service class
    public class TimerService : MarshalByRefObject
 {
        private double m_MinutesToTime;
        private Timer m_Timer;

        // The client will subscribe and unsubscribe to this event
        public event TimerExpiredEventHandler TimerExpired;

        // Default: Initialize the TimerService to 4 minutes, the time
 required
        // to brew coffee in a French Press.
        public TimerService():this(4.0) {
        }
        
        public TimerService(double minutes) {
            Console.WriteLine("TimerService instantiated.");
            m_MinutesToTime = minutes;
            m_Timer = new Timer();
            m_Timer.Elapsed += new ElapsedEventHandler(OnElapsed);
        }
        
        public double MinutesToTime {
            get {
                return m_MinutesToTime;
            }
            set {
                m_MinutesToTime = value;
            }
        }

        public void Start() {
            if(!m_Timer.Enabled) {
                TimeSpan interval = TimeSpan.FromMinutes(m_MinutesToTime);
                m_Timer.Interval = interval.TotalMilliseconds;
                m_Timer.Enabled = true;
            }
            else {
                // TODO: Raise an exception
            }
        }
        
        public void OnElapsed(object source,
 ElapsedEventArgs e) {
            m_Timer.Enabled = false;

            // Fire Event
            if (TimerExpired != null) {
                // Package String in TimerServiceEventArgs
                TimerServiceEventArgs timerEventArgs = new TimerServiceEventArgs("TimerServiceEventArgs:
 Timer Expired.");
                Console.WriteLine("Firing TimerExpired Event");
                TimerExpired(this, timerEventArgs);
            }
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        public override Object InitializeLifetimeService() {
            ILease lease = (ILease)base.InitializeLifetimeService();
            if (lease.CurrentState == LeaseState.Initial) {
                lease.InitialLeaseTime = TimeSpan.FromMinutes(0.125);
                lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
                lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
                Console.WriteLine("TimerService: InitializeLifetimeService");
            }
             return lease;
        }
    }
}

.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RemotingServices クラス
RemotingServices メンバ
System.Runtime.Remoting 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からRemotingServices.GetLifetimeService メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からRemotingServices.GetLifetimeService メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からRemotingServices.GetLifetimeService メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「RemotingServices.GetLifetimeService メソッド」の関連用語

RemotingServices.GetLifetimeService メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



RemotingServices.GetLifetimeService メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS