Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Messaging
'Sample proxy
Public Class MyProxy
Inherits Runtime.Remoting.Proxies.RealProxy
Private myMarshalByRefObject As MarshalByRefObject
Public Sub New(ByVal t As Type)
MyBase.New(t)
myMarshalByRefObject = CType(Activator.CreateInstance(t), MarshalByRefObject)
End Sub
Public Sub New(ByVal myObject As MarshalByRefObject, ByVal t As Type)
MyBase.New(t)
myMarshalByRefObject = myObject
End Sub
'Called during method calls
Public Overrides Function Invoke(ByVal myMessage As IMessage) As IMessage
Dim myMethodCall As IMethodCallMessage = CType(myMessage, IMethodCallMessage)
Dim myCtorCall As Activation.IConstructionCallMessage
Try
'If it ain't a constructor, this throws an exception
myCtorCall = CType(myMethodCall, Activation.IConstructionCallMessage)
Catch ex As Exception
'''
'''Debug.WriteLine("Not a constructor call")
'''
End Try
If Not (myCtorCall Is Nothing) Then
'Log ctor incoming info
Debug.WriteLine("============= -->Proxy Ctor Log =============")
Debug.WriteLine("Ctor, object class=" + myCtorCall.ActivationTypeName())
Debug.WriteLine("# Arguments=" + myCtorCall.ArgCount.ToString())
If myCtorCall.ArgCount > 0 Then
Dim i As Integer
For i = 0 To myCtorCall.ArgCount - 1
Debug.WriteLine("Arg #" + i.ToString() + _
": Name=" + myCtorCall.GetArgName(i) + _
", Value=" + Convert.ToString(myCtorCall.GetArg(i)))
Next
End If
Debug.WriteLine("============= End Proxy Ctor Log =============")
'Run the ctor
RemotingServices.GetRealProxy(myMarshalByRefObject).InitializeServerObject(myCtorCall)
'Log ctor outgoing info
Debug.WriteLine("============= Proxy Ctor Log --> =============")
Dim returnMessage As Activation.IConstructionReturnMessage = _
Services.EnterpriseServicesHelper.CreateConstructionReturnMessage(myCtorCall, _
CType(Me.GetTransparentProxy, MarshalByRefObject))
If Not (returnMessage.Exception Is Nothing) Then
Debug.WriteLine("Ctor has thrown an exception: " + returnMessage.Exception.ToString())
End If
Debug.WriteLine("============= End Proxy Ctor Log =============")
Return returnMessage
Else
'Log incoming method call
Debug.WriteLine("============= --> Proxy Method Log =============")
Debug.WriteLine("Method call, method name=" + myMethodCall.MethodName() + _
", object class=" + myMethodCall.TypeName())
If myMethodCall.InArgCount > 0 Then
Debug.WriteLine("# input arguments: " + myMethodCall.InArgCount.ToString())
Dim i As Integer
For i = 0 To myMethodCall.InArgCount - 1
Debug.WriteLine("Arg #" + i.ToString() + _
": Name=" + myMethodCall.GetInArgName(i) + _
", Value=" + Convert.ToString(myMethodCall.GetInArg(i)))
Next
End If
Debug.WriteLine("============= End Proxy Method Log =============")
'Run the method
Dim returnMessage As IMethodReturnMessage = RemotingServices.ExecuteMessage(myMarshalByRefObject, myMethodCall)
'Log outgoing method call
Debug.WriteLine("============= Proxy Method Log --> =============")
If (Not returnmessage.ReturnValue Is Nothing) AndAlso _
(returnmessage.ReturnValue.GetType().FullName <> "System.Void") Then
Debug.WriteLine("Return type=" + returnmessage.ReturnValue.GetType().FullName + _
", value=" + Convert.ToString(returnmessage.ReturnValue))
End If
If Not (returnMessage.Exception Is Nothing) Then
Debug.WriteLine("Method " + returnmessage.MethodName() + _
" has thrown an exception: " + returnMessage.Exception.ToString())
End If
If returnmessage.OutArgCount > 0 Then
Debug.WriteLine("# output arguments: " + returnmessage.OutArgCount.ToString())
Dim i As Integer
For i = 0 To returnmessage.OutArgCount - 1
Debug.WriteLine("Arg #" + i.ToString() + _
": Name=" + returnmessage.GetoutArgName(i) + _
", Value=" + Convert.ToString(returnmessage.GetoutArg(i)))
Next
End If
Debug.WriteLine("============= End Proxy Method Log =============")
Return returnMessage
End If
Exit Function
End Function
End Class
'Sample proxy attribute
<AttributeUsage(AttributeTargets.Class)> Public Class MyProxyAttribute
Inherits Runtime.Remoting.Proxies.ProxyAttribute
Public Overrides Function CreateInstance(ByVal t As Type) As MarshalByRefObject
Dim obj As MarshalByRefObject, rp As Proxies.RealProxy
obj = MyBase.CreateInstance(t)
rp = New MyProxy(obj, t)
Return CType(rp.GetTransparentProxy(), MarshalByRefObject)
End Function
End Class
'Sample class to be monitored - needs the proxy attribute to instantiate the proxy.
'Also it must inherit from the ContextBoundObject. This might be a problem in certain implementations.
<MyProxyAttribute()> Public Class MyDemoClass
Inherits ContextBoundObject
Public Sub New()
Debug.WriteLine("Ctor called")
End Sub
Public Sub New(ByVal s As String)
Debug.WriteLine("Overloaded ctor called, s=" + s)
End Sub
Public Sub SimpleMethod()
Debug.WriteLine("SimpleMethod called")
End Sub
Public Sub SimpleArgs(ByVal x As Integer)
Debug.WriteLine("SimpleArgs called, x=" + x.ToString())
End Sub
Public Function SimpleReturn(ByVal x As Integer) As Integer
Debug.WriteLine("SimpleReturn called, x=" + x.ToString() + ", returning " + (x * 2).ToString())
Return x * 2
End Function
Public Sub SimpleByRef(ByVal x As Integer, ByRef y As Integer)
Debug.WriteLine("SimpleByRef called, x=" + x.ToString() + ", y= " + (x * 2).ToString())
y = x * 2
End Sub
Public Sub SimpleException()
Throw New NotSupportedException("Exception message")
End Sub