VB.Net/Network Remote/Remote Factories — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:45, 26 мая 2010
Acquire Remote Object from Remote Object Factory
///////////////////////////////////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
Public Class MyRemoteObject
Inherits MarshalByRefObject
Implements IRemoteObject
Private myvalue As Integer
Public Sub New()
End Sub "New
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
Public Class MyRemoteFactory
Inherits MarshalByRefObject
Implements IRemoteFactory
Public Sub New()
End Sub
Public Function getNewInstance() As IRemoteObject _
Implements IRemoteFactory.getNewInstance
Return New MyRemoteObject()
End Function
Public Function getNewInstance(ByVal initvalue As Integer) As IRemoteObject _
Implements IRemoteFactory.getNewInstance
Return New MyRemoteObject(initvalue)
End Function
End Class
///////////////////////////////////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)
Console.WriteLine("Client.Main(): Creating factory")
Dim fact As IRemoteFactory = CType(Activator.GetObject( _
GetType(IRemoteFactory), _
"http://localhost:1234/factory.soap"), _
IRemoteFactory)
Console.WriteLine("Client.Main(): Acquiring first object from factory")
Dim obj1 As IRemoteObject = fact.getNewInstance()
obj1.setValue(42)
Console.WriteLine("Client.Main(): Acquiring second object from factory")
Dim obj2 As IRemoteObject = fact.getNewInstance(47)
Console.WriteLine("Obj1.getValue(): {0}", obj1.getValue())
Console.WriteLine("Obj2.getValue(): {0}", obj2.getValue())
Console.ReadLine()
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
Module ServerStartup
Sub Main()
Dim chnl As New HttpChannel(1234)
ChannelServices.RegisterChannel(chnl,false)
RemotingConfiguration.RegisterWellKnownServiceType( _
GetType(MyRemoteFactory), _
"factory.soap", _
WellKnownObjectMode.Singleton)
Console.WriteLine("ServerStartup.Main(): Server started")
Console.ReadLine()
End Sub
End Module