本文整理汇总了VB.NET中System.Runtime.Remoting.RemotingServices.GetLifetimeService方法的典型用法代码示例。如果您正苦于以下问题:VB.NET RemotingServices.GetLifetimeService方法的具体用法?VB.NET RemotingServices.GetLifetimeService怎么用?VB.NET RemotingServices.GetLifetimeService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了RemotingServices.GetLifetimeService方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: TimerClient
' 导入命名空间
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
<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
Private Sub OnTimerExpired(ByVal [source] As Object, ByVal e As TimerServiceEventArgs)
Console.WriteLine("TimerHelper.OnTimerExpired: {0}", e.Message)
End Sub
<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
示例2: Main
' 导入命名空间
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
End Class
示例3: New
' 导入命名空间
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
Public ReadOnly Property Message() As String
Get
Return m_Message
End Get
End Property
End Class
' 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
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
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
Private 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
<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