VB.Net/Network Remote/Remote Basics

Материал из VB Эксперт
Перейти к: навигация, поиск

Call remote object method to Get and set variable

<source lang="vbnet"> ///////////////////////////////////general.vb // Compile: vbc /target:library general.vb Imports System Public Class MyRemoteObject

   Inherits MarshalByRefObject
   Private myvalue As Integer
   Public Sub New()
       Console.WriteLine("MyRemoteObject.Constructor: New Object created")
   End Sub "New
   Public Sub New(ByVal startvalue As Integer)
       Console.WriteLine("MyRemoteObject.Constructor: .ctor called with {0}", _
           startvalue)
       myvalue = startvalue
   End Sub
   Public Sub setValue(ByVal newval As Integer)
       Console.WriteLine("MyRemoteObject.setValue(): old {0} new {1}", _
           myvalue, newval)
       myvalue = newval
   End Sub
   Public Function getValue() As Integer
       Console.WriteLine("MyRemoteObject.getValue(): current {0}", _
           myvalue)
       Return myvalue
   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)
       RemotingConfiguration.RegisterActivatedClientType( _
           GetType(MyRemoteObject), "http://localhost:1234/MyServer")
       Console.WriteLine("Client.Main(): Creating first object")
       Dim obj1 As New MyRemoteObject()
       obj1.setValue(42)
       Console.WriteLine("Client.Main(): Creating second object")
       Dim obj2 As New MyRemoteObject()
       obj2.setValue(11)
       Console.WriteLine("Obj1.getValue(): {0}", obj1.getValue())
       Console.WriteLine("Obj2.getValue(): {0}", obj2.getValue())
   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()
       Console.WriteLine("ServerStartup.Main(): Server started")
       Dim chnl As New HttpChannel(1234)
       ChannelServices.RegisterChannel(chnl,false)
       RemotingConfiguration.ApplicationName = "MyServer"
       RemotingConfiguration.RegisterActivatedServiceType( _
           GetType(MyRemoteObject))


       Console.ReadLine()
   End Sub

End Module


      </source>


Validate Data remotely

<source lang="vbnet"> ///////////////////////////////////general.vb // Compile: vbc /target:library general.vb Imports System Public Interface IEmployeeManager

   Function getEmployee(ByVal id As Integer) As Employee
   Function validate(ByVal cust As Employee) As ValidationResult

End Interface

<Serializable()> _ Public Class ValidationResult

   Public Sub New(ByVal ok As Boolean, ByVal msg As String)
       Console.WriteLine("ValidationResult: Object created")
       Me.Ok = ok
       Me.ValidationMessage = msg
   End Sub "New
   Public Ok As Boolean
   Public ValidationMessage As String

End Class <Serializable()> _ Public Class Employee

   Public FirstName As String
   Public LastName As String
   Public DateOfBirth As DateTime
   Public Sub New()
       Console.WriteLine("Employee.constructor: Object created")
   End Sub "New
   Public Function getAge() As Integer
       Dim tmp As TimeSpan = DateTime.Today.Subtract(DateOfBirth)
       Return tmp.Days \ 365 " rough estimation
   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)
       Dim mgr As IEmployeeManager = CType(Activator.GetObject( _
           GetType(IEmployeeManager), "http://localhost:1234/EmployeeManager.soap"), _
           IEmployeeManager)
       Dim cust As New Employee()
       cust.FirstName = "James"
       cust.LastName = "Band"
       cust.DateOfBirth = New DateTime(1999, 5, 12)
       Dim res As ValidationResult = mgr.validate(cust)
       Console.WriteLine("Validation result for {0} {1}" & vbCrLf & _
           "-> {2}: {3}", cust.FirstName, cust.LastName, res.Ok.ToString(), _
           res.ValidationMessage)
   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 Class EmployeeManager

   Inherits MarshalByRefObject
   Implements IEmployeeManager
   Public Sub New()
       Console.WriteLine("EmployeeManager: Object created")
   End Sub
   Public Function getEmployee(ByVal id As Integer) As Employee _
   Implements IEmployeeManager.getEmployee
       Dim tmp As New Employee()
       tmp.FirstName = "James"
       tmp.LastName = "Band"
       tmp.DateOfBirth = New DateTime(1989, 7, 4)
       Return tmp
   End Function
   Public Function validate(ByVal cust As Employee) As ValidationResult _
   Implements IEmployeeManager.validate
       Dim age As Integer = cust.getAge()
       Console.WriteLine("EmployeeManager.validate() for {0} aged {1}", _
           cust.FirstName, age)
       If cust.FirstName Is Nothing Or cust.FirstName.Length = 0 Then
           Return New ValidationResult(False, "Firstname missing")
       End If
       If cust.LastName Is Nothing Or cust.LastName.Length = 0 Then
           Return New ValidationResult(False, "Lastname missing")
       End If
       If age < 0 Or age > 120 Then
           Return New ValidationResult(False, "Employee must be younger than 120 years")
       End If
       Return New ValidationResult(True, "Validation succeeded")
   End Function

End Class Module ServerStartup

   Sub Main()
       Console.WriteLine("ServerStartup.Main(): Server started")
       Dim chnl As New HttpChannel(1234)
       ChannelServices.RegisterChannel(chnl,false)
       RemotingConfiguration.RegisterWellKnownServiceType( _
           GetType(EmployeeManager), _
           "EmployeeManager.soap", _
           WellKnownObjectMode.Singleton)
       
       Console.ReadLine()
   End Sub

End Module


      </source>


Your first Remote client and server

<source lang="vbnet"> ///////////////////////////////RemoteObject: general.vb // Complie: vbc /target:library general.vb

Public Interface IEmployeeManager

   Function getEmployee(ByVal id As Integer) As Employee

End Interface <Serializable()> _ Public Class Employee

   Public FirstName As String
   Public LastName As String
   Public DateOfBirth As DateTime
   Public Sub New()
       Console.WriteLine("Employee.constructor: Object created")
   End Sub "New
   Public Function getAge() As Integer
       Console.WriteLine("In getAge Method")
       Return 30
   End Function

End Class

/////////////////////////////test.vb // Complie: 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 Imports System.Runtime.Remoting.Proxies Imports System.Runtime.Remoting.Messaging

Module Client

   Sub Main()
       Dim channel As New HttpChannel()
       ChannelServices.RegisterChannel(channel,false)
       Dim mgr As IEmployeeManager = CType(Activator.GetObject( _
           GetType(IEmployeeManager), "http://localhost:1234/EmployeeManager.soap"), _
           IEmployeeManager)
       Console.WriteLine("Client.Main(): Reference to EmployeeManager acquired")
       Dim cust As Employee = mgr.getEmployee(4711)
       Dim age As Integer = cust.getAge()
       Console.WriteLine("Client.Main(): Employee {0} {1} is {2} years old.", _
           cust.FirstName, cust.LastName, age)
       Console.ReadLine()
   End Sub

End Module

///////////////////////////server.vb // Complie: 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 Class EmployeeManager

   Inherits MarshalByRefObject
   Implements IEmployeeManager
   Public Sub New()
       Console.WriteLine("EmployeeManager.constructor: Object created")
   End Sub
   Public Function getEmployee(ByVal id As Integer) As Employee _
   Implements IEmployeeManager.getEmployee
       Console.WriteLine("EmployeeManager.getEmployee): Called")
       Dim tmp As New Employee()
       tmp.FirstName = "James"
       tmp.LastName = "Band"
       tmp.DateOfBirth = New DateTime(1007, 7, 4)
       Console.WriteLine(("EmployeeManager.getEmployee(): Returning " & _
           "Employee-Object"))
       Return tmp
   End Function

End Class Module ServerStartup

   Sub Main()
       Console.WriteLine("ServerStartup.Main(): Server started")
       Dim chnl As New HttpChannel(1234)
       ChannelServices.RegisterChannel(chnl,false)
       RemotingConfiguration.RegisterWellKnownServiceType( _
           GetType(EmployeeManager), _
           "EmployeeManager.soap", _
           WellKnownObjectMode.Singleton)
       
       Console.ReadLine()
   End Sub

End Module


      </source>