In SAO, SingleCall remote object activation, each remote
method call creates new instance of the remote object at the server end. When a
client finishes its remote calls, the server deletes object instance which was
created for it. Therefore, we cannot do state management at the server among
multiple clients. In other words, we cannot share variable or manage state
among different calls since a new object initializes for each client remote
method call. It also means that the server object constructor New() executes
for each remote object creation at the client so as for each remote method call
by client.
For example, if client A halts at some line of code in the
execution then a call from other client (say B, C, D, etc.) does not wait in
any queue at the server and so does not wait for client A to finish its
execution. All executes are an independent thread.
Now we will see how we can implement SAO in .NET. For better
understanding I have separated the remote object creation code and remote
class/method implementation code in Module and Class file respectively in this
example. There are few commented lines which I used in my tests, please remove
it if you want.
Listing 1: SAO (Well-known)-SingleCall Module and Main() implementation for server in VB.NET (Module1.vb)
Imports System
Imports System.IO
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Module Module1
Sub Main()
' ------- SAO Singleton ----------
Dim channel As HttpChannel = New HttpChannel(1)
Try
ChannelServices.RegisterChannel(channel, False)
Dim serverType As Type = Type.GetType("RemoteServer.CServer")
RemotingConfiguration.RegisterWellKnownServiceType(serverType, _
"TestRemoteServer", WellKnownObjectMode.SingleCall)
Console.WriteLine("Remote Service started...Press Enter key to stop." _
& Now.ToString())
Console.Read()
Catch ex As Exception
Console.WriteLine(ex.ToString())
Console.Read()
ChannelServices.UnregisterChannel(channel)
Finally
End Try
' ---------------------------------
End Sub
End Module
Listing 2: SAO (Well-known)-SingleCall Class
implementation for server in VB.NET (Server.vb)
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Security.Permissions
Imports System.Runtime.Remoting.Lifetime
Public Class CServer
Inherits MarshalByRefObject
Implements ILibrary.IServer
Public sClientCallsInOrder As Integer = 0
Public Function GetServerMessage(ByVal ClientName As String) As String _
Implements ILibrary.IServer.GetServerMessage
sClientCallsInOrder = sClientCallsInOrder + 1
Console.WriteLine("**Call By :: " & sClientCallsInOrder & " :: " & _
ClientName & " :: " & Now.ToString())
Dim ResponseMessage As String = Nothing
If Not ClientName Is Nothing Then
If ClientName.ToUpper() = "CLIENT_A" Then
Dim x
x = 10
' Threading.Thread.Sleep(20000)
End If
ResponseMessage = "Hello " & ClientName & "! Welcome at Remote Server"
End If
'sClientCallsInOrder = sClientCallsInOrder & " :: [" & ClientName & "]"
Console.WriteLine(" Finish :: " & sClientCallsInOrder & " :: " & _
ClientName & " :: " & Now.ToString())
Return (ResponseMessage)
End Function
Public Sub New()
Console.WriteLine("Object constructor called :: " & Now.ToString())
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class