ARTICLE

Common Interfaces using VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 28, 2010
In this article I will explain you about Common Interfaces using VB.NET.
 
Reader Level:

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.

share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor