Call Contexts in VB.NET

In this article I will explain you about Call Contexts in VB.NET.
  • 1974

The CallContext class can be thought of as "out-of-band" data or a channel hook. It operates at a thread level within an application domain. To cross application domain boundaries, objects must derive from the System.Runtime.Remoting.Messaging.ILogicalThreadAffinative interface, see Listing 25.18. An object registers with CallContext using the "key/value" paradigm. It is hooked to each method call and is part of IMessage, the _CallContext entry, which is sent over the wire. Using CallContext is simply a matter of calling the static methods SetData() and GetData().

There is a standard library (CallContextLib) and two console applications (CallContextServer and CallContextClient) after creating these two console applications you need to create a application named RemoteContextInfo it is the class that will contain the out-of-band data. Here I will given the example of  RemoteContextInfo class and when you use this class with CallContextServer and CallContextClient application the output which is produced is given in Figures 25.13 and 25.14 are the client and server output, respectively.

Listing 25.18: RemoteContextInfo.vb

<Serializable()> _
Public Class RemoteContextInfo
    Implements ILogicalThreadAffinative
    Private m_machineName As String = Nothing
    Private m_clientDir As String = Nothing
    Public Sub New() 
        m_machineName = Environment.MachineName
        m_clientDir = Environment.CurrentDirectory
    End Sub
    Public ReadOnly Property MachineName() As String
 
        Get
            Return (m_machineName)
        End Get
    End Property
    Public ReadOnly Property ClientDir() As String
        Get
            Return (m_clientDir)
        End Get
    End Property
End Class

To get and set the call context data, simply use the following code:

        Dim data As RemoteContextInfo = Nothing
       
data = New RemoteContextInfo()
        CallContext.SetData("Client", data)
        Dim data As RemoteContextInfo = DirectCast(CallContext.GetData("Client"), RemoteContextInfo)

Figure 25.13: Client Output

Figure-1.gif

Figure 25.14: Server Output

Figure-2.gif

This is an ideal way for custom proxies or message sinks to pass information from the client to the server or vice versa, without intruding into the functionality of the application.

Conclusion

Hope this article would have helped you in understanding Call Contexts in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.