Events and Delegates in Remoting using VB.NET
In this article I will explain you about Events and Delegates in Remoting using VB.NET.
Event notification in the remoting framework employs the same paradigm as the rest of the .NET Framework: delegates and events. There are minor differences in construction; and we should point out a constraint before proceeding.
The event originator cannot be registered as a server-activated object in SingleCall mode. Because the object is re-created with each method call, it won't be able to retain a reference to the event's delegate.
The projects are in the Sample3 folder and consist of SimpleEventLib.dll and two console applications, EventServer and EventClient. The delegate, with its method signature, and the event arguments class code are shown in Listing 25.11.
Listing 25.11:RemoteArgs.vb
Public Delegate Sub RemoteEventHandler(ByVal sender As Object, ByVal args As RemoteArgs)
<Serializable()> _
Public Class RemoteArgs
Private m_machineName As String = Nothing
Private m_currentDir As String = Nothing
Friend Sub New()
m_machineName = Environment.MachineName
m_currentDir = Environment.CurrentDirectory
End Sub
Public ReadOnly Property MachineName() As String
Get
Return (m_machineName)
End Get
End Property
Public ReadOnly Property CurrentDir() As String
Get
Return (m_currentDir)
End Get
End Property
End ClassPublic Delegate Sub RemoteEventHandler(ByVal sender As Object, ByVal args As RemoteArgs)
<Serializable()> _
Public Class RemoteArgs
Private m_machineName As String = Nothing
Private m_currentDir As String = Nothing
Friend Sub New()
m_machineName = Environment.MachineName
m_currentDir = Environment.CurrentDirectory
End Sub
Public ReadOnly Property MachineName() As String
Get
Return (m_machineName)
End Get
End Property
Public ReadOnly Property CurrentDir() As String
Get
Return (m_currentDir)
End Get
End Property
End Class
The delegate should come as no surprise; it looks like any other event delegate. The RemoteArgs class doesn't derive from System.EventArgs. To compensate for the lack of an EventArgs parent, the [Serializable] attribute has been attached so it can be marshaled across application domains. The event sink, Listing 25.12, derives from MarshalByRefObject and implements an event handler.
Listing 25.12: SyncRemoteEvent.vb
Public Class SyncRemoteEvent
Inherits MarshalByRefObject
Public Sub New()
MyBase.New()
Console.WriteLine("In SyncRemoteEvent constructor")
End Sub
Public Overridable Sub EventHandler(ByVal send As Object, ByVal arg As RemoteArgs)
Console.WriteLine()
Console.WriteLine("In SyncRemoteEvent.EventHandler")
Console.WriteLine("Machine:{0} Dir:{1}", args.MachineName, args.CurrentDir)
Console.WriteLine()
End Sub
End Class
SimpleObject has changed very little, except that we have added an event delegate, Listing 25.13.
Listing 25.13: SimpleObject.vb
public class SimpleObject
Inherits MarshalByRefObject
public Sub New()
.
.
.
In the ConcatString() we insert the event-handling code shown in Listing 25.14 to test whether any remote objects are subscribing to the event. If a subscriber exists, a RemoteArgs instance is created and the event handler is called.
Listing 25.14: SimpleObject.vb
Public Function ConCatString(ByVal first As String, ByVal second As String) As String
Dim concat As String = Nothing
Console.WriteLine("In SimpleObject.ConCatString method.")
If RemoteEvent IsNot Nothing Then
Dim args As RemoteArgs = Nothing
args = New RemoteArgs()
RemoteEvent(Me, args)
End If
concat = first & " " & Second()
Return (concat)
End Function
Conclusion
Hope this article would have helped you in understanding Events and Delegates in Remoting using VB.NET. Remaining part of this article you will see in my next article.