Initialize Lifetime Service (bugs) : Remote Life Time « Network Remote « VB.Net






Initialize Lifetime Service (bugs)

///////////////////////////////////general.vb
// Compile: vbc /target:library  general.vb
Imports System

Public Interface IRemoteObject
    Sub setValue(ByVal newval As Integer)
    Function getValue() As Integer
End Interface

Public Interface IRemoteFactory
    Overloads Function getNewInstance() As IRemoteObject
    Overloads Function getNewInstance(ByVal initvalue As Integer) As IRemoteObject
End Interface



///////////////////////////////////test.vb
// Compile: vbc /t:exe /r:general.dll test.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels

Module Client

    Sub Main()
        Dim channel As New HttpChannel()
        ChannelServices.RegisterChannel(channel,false)

        Dim fact As IRemoteFactory = CType(Activator.GetObject( _
            GetType(IRemoteFactory), _
            "http://localhost:1234/factory.soap"), _
            IRemoteFactory)

        Dim obj1 As IRemoteObject = fact.getNewInstance()

'        System.Threading.Thread.Sleep(1000)
        Console.WriteLine("call remote method")

        Try
            obj1.setValue(42)
        Catch e As Exception
            Console.WriteLine("Client.Main(). EXCEPTION " + vbCrLf + e.Message)
        End Try

    End Sub

End Module

///////////////////////////////////server.vb
// vbc /target:exe  /r:general.dll server.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Lifetime

Class MyRemoteObject
    Inherits MarshalByRefObject
    Implements IRemoteObject

    Private myvalue As Integer


    Public Overrides Function InitializeLifetimeService() As Object
        Dim lease As ILease = CType(MyBase.InitializeLifetimeService(), ILease)

        If lease.CurrentState = LeaseState.Initial Then
            lease.InitialLeaseTime = TimeSpan.FromMilliseconds(10)
            lease.SponsorshipTimeout = TimeSpan.FromMilliseconds(10)
            lease.RenewOnCallTime = TimeSpan.FromMilliseconds(10)
        End If

        Return lease
    End Function


    Public Sub New()
       myvalue = 0
    End Sub

    Public Sub New(ByVal startvalue As Integer)
        myvalue = startvalue
    End Sub

    Public Sub setValue(ByVal newval As Integer) _
    Implements IRemoteObject.setValue
        myvalue = newval
    End Sub

    Public Function getValue() As Integer _
    Implements IRemoteObject.getValue
        Return myvalue
    End Function

End Class

Class MyRemoteFactory
    Inherits MarshalByRefObject
    Implements IRemoteFactory

    Public Function getNewInstance() As IRemoteObject _
    Implements IRemoteFactory.getNewInstance
        Console.WriteLine("create object with no argment")
        Return New MyRemoteObject()
    End Function

    Public Function getNewInstance(ByVal initvalue As Integer) As IRemoteObject _
    Implements IRemoteFactory.getNewInstance
        Console.WriteLine("create object with argument")
        Return New MyRemoteObject(initvalue)
    End Function


End Class

Module ServerStartup

    Sub Main()
        LifetimeServices.LeaseManagerPollTime = TimeSpan.FromMilliseconds(10)

        Dim chnl As New HttpChannel(1234)
        ChannelServices.RegisterChannel(chnl,false)

        RemotingConfiguration.RegisterWellKnownServiceType( _
            GetType(MyRemoteFactory), _
            "factory.soap", _
            WellKnownObjectMode.Singleton)

        Console.WriteLine("Server started")
        Console.ReadLine()

    End Sub
End Module



           
       








Related examples in the same category