Singleton Objects with SOAP using VB.NET

In this article I will explain you about Singleton Objects with SOAP using VB.NET.
  • 2058

As you learn in my previous article the term singleton is a concept where a global object services all requests. The Singleton concept is a taken from Design Patterns. It is similar to CWinApp in Microsoft Foundation Classes-the one and only application object. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager.

Now we talk about Singleton Objects with SOAP, there are two well-known object modes in SOAP .NET Remoting:

  • SingleCall: Every message is dispatched to a new object instance.

  • Singleton: Every message is dispatched to the same object instance.

Listing 23.12 displays the sample server code.

Listing 23.12: SOAP Server2 (Listing23.12.vb)


// compile with:
// csc /r:object2.dll Listing23.12.vb
/* sample output:
press <enter> to exit
DateTime server activated

Counter: 1
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 2
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 3
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 4
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 5
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 6
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 7
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 8
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 9
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

Counter: 10
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM
DateTime server Object Destroyed
*/
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http 
Namespace ExampleRemoting
    Public Class Example
        Public Shared Sub Main()
            ChannelServices.RegisterChannel(New HttpChannel(999))
            RemotingConfiguration.RegisterWellKnownServiceType(Type.[GetType
            ("ExampleRemoting.DateTimeServer, object2"), "server/SayDateTime.SOAP",
            WellKnownObjectMode.Singleton)
            System.Console.WriteLine("press <enter> to exit.")
            System.Console.ReadLine()
        End Sub
    End Class
End Namespace

Listing 23.13 shows the sample client code. Note that we will use HTTP channels to connect to the remote object twice in a row.

Listing 23.13: SOAP Client2 (Listing23.13.vb)


/* after executing the Listing23.12.exe use the SOAPSUDS tool to create object2.dll
proxy:
soapsuds -url:http://127.0.0.1:999/server/SayDateTime.SOAP?WSDL -oa:object22.dll
*/
// compile with:
// csc /r:object22.dll Listing23.13.vb
/* sample output:
HttpChannel

1 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

2 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

3 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

4 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

5 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM
HttpChannel

6 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

7 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

8 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

9 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM

10 - HttpChannel
Hi Clown Bozo. Here is the current DateTime: 8/19/2002 4:02:28 AM
*/

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)
            Run()
            ' run 1st, singleton counter counts
            Run()
            ' run 2nd, singleton counter counts
        End Sub
 
        Public Shared Sub Run()
            Console.WriteLine("HttpChannel.")
            Dim obj As DateTimeServer = DirectCast(Activator.GetObject(GetType(DateTimeServer),
                "http://127.0.0.1:999/server/SayDateTime.SOAP"), DateTimeServer)
            For i As Integer = 0 To 4
                Console.WriteLine(obj.BeniSay() & " - HttpChannel")
                Console.WriteLine(obj.MyMethod("Clown Bozo"))
            Next
        End Sub
    End Class
End Namespace

Conclusion

Hope this article would have helped you in understanding Singleton Objects with SOAP using VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.