Let's see another aspect of .NET Remoting. This will put you in mind of the COM+
style of remote object usage. We will develop an example using a shared
interface. The interfaces also afford us a good means for using black-box
objects. We have used this feature with type library (TLB) files with COM+
objects. To develop against a type library is sufficient to use services on a
COM server. The client calls GetObject on an endpoint without knowing what the
exact object type is; all it knows is that the object implements an interface.
This illustrates how we can build a client that does not reference a server
object at compile-time. The sample code for this example consists of four parts:
a shared interface, the remote object that implements this interface, a server,
and a client that will use this interface method via an unknown object. Listing
23.17 displays the code for the shared interface.
Listing 23.17: SOAP Shared Interface3 (Listing23.17.VB)
Imports System
Namespace
ExampleRemoting
Public
Interface IDateTime
Function
DateTimeMethod(name As [String])
As
String
End Interface
End Namespace
Listing 23.18 contains the code for the object that implements the shared
interface.
Listing 23.18: SOAP Object That Implements Share 3 (Listing23.18.VB)
Namespace
ExampleRemoting
Public Class DateTimeServer
Inherits MarshalByRefObject
Implements IDateTime
Public
Sub New()
Console.WriteLine("DateTime
server activated")
End Sub
Protected Overrides Sub
Finalize()
Try
Console.WriteLine("DateTime
server Object Destroyed")
Finally
MyBase.Finalize()
End
Try
End Sub
Public Function DateTimeMethod(ByVal
name As [String])
As [String]
Dim strMessage
As [String]
= "Hi " + name +
". Here is the current DateTime: " +
DateTime.Now
Console.WriteLine(strMessage)
Return strMessage
End
Function
End
Class
End Namespace
The code for the server that delivers the remote methods appears in Listing
23.19.
Listing 23.19: SOAP Server3 (Listing23.19.VB)
Imports System
Imports
System.Runtime.Remoting
Imports
System.Runtime.Remoting.Channels
Imports
System.Runtime.Remoting.Channels.Http
Namespace
ExampleRemoting
Public Class Example
Public Shared Sub
Main()
Dim channel
As New
HttpChannel(8888)
ChannelServices.RegisterChannel(channel)
RemotingConfiguration.RegisterWellKnownServiceType(Type.[GetType]("ExampleRemoting.DateTimeServer,
Object3"), "SayDateTime",
WellKnownObjectMode.SingleCall)
System.Console.WriteLine("press
<enter> to exit.")
System.Console.ReadLine()
End Sub
End
Class
End
Namespace
Listing 23.20 shows the code for the client that connects to the server and uses
the methods exposed.
Listing 23.20: SOAP Client3 (Listing23.20.VB)
Imports System
Imports System
Imports
System.Runtime.Remoting
Imports
System.Runtime.Remoting.Channels
Imports
System.Runtime.Remoting.Channels.Http
Namespace
ExampleRemoting
Public Class Client
Public Shared Sub
Main()
Dim channel
As New
HttpChannel()
ChannelServices.RegisterChannel(channel)
' create an object of type interface
Dim obj
As IDateTime =
DirectCast(Activator.GetObject(GetType(ExampleRemoting.IDateTime),
"http://127.0.0.1:8888/SayDateTime"),
IDateTime)
If obj =
Nothing Then
Console.WriteLine("could
not locate server!")
Else
Console.WriteLine(obj.DateTimeMethod("Bozo
the clown"))
End
If
End Sub
End
Class
End Namespace
Namespace
ExampleRemoting
Public Class Client
Public Shared Sub
Main()
Dim channel
As New
HttpChannel()
ChannelServices.RegisterChannel(channel)
' create an object of type interface
Dim obj
As IDateTime =
DirectCast(Activator.GetObject(GetType(ExampleRemoting.IDateTime),
"http://127.0.0.1:8888/SayDateTime"),
IDateTime)
If obj =
Nothing Then
Console.WriteLine("could
not locate server!")
Else
Console.WriteLine(obj.DateTimeMethod("Bozo
theclown"))
End
If
End Sub
End
Class
End Namespace
Conclusion
Hope this
article would have helped you in understanding Common Interfaces using VB.NET.