Results 1 to 2 of 2

Thread: RealProxy [RESOLVED]

Threaded View

  1. #1

    Thread Starter
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Resolved RealProxy [RESOLVED]

    I'm trying to use something like the following in a remoting scenario.

    VB Code:
    1. Imports System.Runtime.Remoting
    2. Imports System.Runtime.Remoting.Messaging
    3.  
    4. 'Sample proxy
    5. Public Class MyProxy
    6.     Inherits Runtime.Remoting.Proxies.RealProxy
    7.  
    8.     Private myMarshalByRefObject As MarshalByRefObject
    9.  
    10.     Public Sub New(ByVal t As Type)
    11.  
    12.         MyBase.New(t)
    13.         myMarshalByRefObject = CType(Activator.CreateInstance(t), MarshalByRefObject)
    14.  
    15.     End Sub
    16.  
    17.     Public Sub New(ByVal myObject As MarshalByRefObject, ByVal t As Type)
    18.  
    19.         MyBase.New(t)
    20.         myMarshalByRefObject = myObject
    21.  
    22.     End Sub
    23.  
    24.     'Called during method calls
    25.     Public Overrides Function Invoke(ByVal myMessage As IMessage) As IMessage
    26.  
    27.         Dim myMethodCall As IMethodCallMessage = CType(myMessage, IMethodCallMessage)
    28.         Dim myCtorCall As Activation.IConstructionCallMessage
    29.  
    30.         Try
    31.             'If it ain't a constructor, this throws an exception
    32.             myCtorCall = CType(myMethodCall, Activation.IConstructionCallMessage)
    33.         Catch ex As Exception
    34.             '''
    35.             '''Debug.WriteLine("Not a constructor call")
    36.             '''
    37.         End Try
    38.  
    39.         If Not (myCtorCall Is Nothing) Then
    40.  
    41.             'Log ctor incoming info
    42.             Debug.WriteLine("============= -->Proxy Ctor Log =============")
    43.             Debug.WriteLine("Ctor, object class=" + myCtorCall.ActivationTypeName())
    44.             Debug.WriteLine("# Arguments=" + myCtorCall.ArgCount.ToString())
    45.  
    46.             If myCtorCall.ArgCount > 0 Then
    47.                 Dim i As Integer
    48.                 For i = 0 To myCtorCall.ArgCount - 1
    49.                     Debug.WriteLine("Arg #" + i.ToString() + _
    50.                                     ": Name=" + myCtorCall.GetArgName(i) + _
    51.                                     ", Value=" + Convert.ToString(myCtorCall.GetArg(i)))
    52.                 Next
    53.             End If
    54.  
    55.             Debug.WriteLine("============= End Proxy Ctor Log =============")
    56.  
    57.             'Run the ctor
    58.             RemotingServices.GetRealProxy(myMarshalByRefObject).InitializeServerObject(myCtorCall)
    59.  
    60.             'Log ctor outgoing info
    61.             Debug.WriteLine("============= Proxy Ctor Log --> =============")
    62.  
    63.             Dim returnMessage As Activation.IConstructionReturnMessage = _
    64.                 Services.EnterpriseServicesHelper.CreateConstructionReturnMessage(myCtorCall, _
    65.                 CType(Me.GetTransparentProxy, MarshalByRefObject))
    66.  
    67.             If Not (returnMessage.Exception Is Nothing) Then
    68.                 Debug.WriteLine("Ctor has thrown an exception: " + returnMessage.Exception.ToString())
    69.             End If
    70.  
    71.             Debug.WriteLine("============= End Proxy Ctor Log =============")
    72.  
    73.             Return returnMessage
    74.  
    75.         Else
    76.  
    77.             'Log incoming method call
    78.             Debug.WriteLine("============= --> Proxy Method Log =============")
    79.             Debug.WriteLine("Method call, method name=" + myMethodCall.MethodName() + _
    80.                             ", object class=" + myMethodCall.TypeName())
    81.  
    82.             If myMethodCall.InArgCount > 0 Then
    83.                 Debug.WriteLine("# input arguments: " + myMethodCall.InArgCount.ToString())
    84.                 Dim i As Integer
    85.                 For i = 0 To myMethodCall.InArgCount - 1
    86.                     Debug.WriteLine("Arg #" + i.ToString() + _
    87.                                     ": Name=" + myMethodCall.GetInArgName(i) + _
    88.                                     ", Value=" + Convert.ToString(myMethodCall.GetInArg(i)))
    89.                 Next
    90.             End If
    91.  
    92.             Debug.WriteLine("============= End Proxy Method Log =============")
    93.  
    94.             'Run the method
    95.             Dim returnMessage As IMethodReturnMessage = RemotingServices.ExecuteMessage(myMarshalByRefObject, myMethodCall)
    96.  
    97.             'Log outgoing method call
    98.             Debug.WriteLine("============= Proxy Method Log --> =============")
    99.  
    100.             If (Not returnmessage.ReturnValue Is Nothing) AndAlso _
    101.                (returnmessage.ReturnValue.GetType().FullName <> "System.Void") Then
    102.                 Debug.WriteLine("Return type=" + returnmessage.ReturnValue.GetType().FullName + _
    103.                                 ", value=" + Convert.ToString(returnmessage.ReturnValue))
    104.             End If
    105.  
    106.             If Not (returnMessage.Exception Is Nothing) Then
    107.                 Debug.WriteLine("Method " + returnmessage.MethodName() + _
    108.                                 " has thrown an exception: " + returnMessage.Exception.ToString())
    109.             End If
    110.  
    111.             If returnmessage.OutArgCount > 0 Then
    112.                 Debug.WriteLine("# output arguments: " + returnmessage.OutArgCount.ToString())
    113.                 Dim i As Integer
    114.                 For i = 0 To returnmessage.OutArgCount - 1
    115.                     Debug.WriteLine("Arg #" + i.ToString() + _
    116.                                     ": Name=" + returnmessage.GetoutArgName(i) + _
    117.                                     ", Value=" + Convert.ToString(returnmessage.GetoutArg(i)))
    118.                 Next
    119.             End If
    120.  
    121.             Debug.WriteLine("============= End Proxy Method Log =============")
    122.  
    123.             Return returnMessage
    124.  
    125.         End If
    126.  
    127.         Exit Function
    128.  
    129.     End Function
    130. End Class
    131.  
    132. 'Sample proxy attribute
    133. <AttributeUsage(AttributeTargets.Class)> Public Class MyProxyAttribute
    134.  
    135.     Inherits Runtime.Remoting.Proxies.ProxyAttribute
    136.  
    137.     Public Overrides Function CreateInstance(ByVal t As Type) As MarshalByRefObject
    138.  
    139.         Dim obj As MarshalByRefObject, rp As Proxies.RealProxy
    140.  
    141.         obj = MyBase.CreateInstance(t)
    142.         rp = New MyProxy(obj, t)
    143.         Return CType(rp.GetTransparentProxy(), MarshalByRefObject)
    144.  
    145.     End Function
    146.  
    147. End Class
    148.  
    149. 'Sample class to be monitored - needs the proxy attribute to instantiate the proxy.
    150. 'Also it must inherit from the ContextBoundObject. This might be a problem in certain implementations.
    151. <MyProxyAttribute()> Public Class MyDemoClass
    152.     Inherits ContextBoundObject
    153.  
    154.     Public Sub New()
    155.         Debug.WriteLine("Ctor called")
    156.     End Sub
    157.  
    158.     Public Sub New(ByVal s As String)
    159.         Debug.WriteLine("Overloaded ctor called, s=" + s)
    160.     End Sub
    161.  
    162.     Public Sub SimpleMethod()
    163.         Debug.WriteLine("SimpleMethod called")
    164.     End Sub
    165.  
    166.     Public Sub SimpleArgs(ByVal x As Integer)
    167.         Debug.WriteLine("SimpleArgs called, x=" + x.ToString())
    168.     End Sub
    169.  
    170.     Public Function SimpleReturn(ByVal x As Integer) As Integer
    171.         Debug.WriteLine("SimpleReturn called, x=" + x.ToString() + ", returning " + (x * 2).ToString())
    172.         Return x * 2
    173.     End Function
    174.  
    175.     Public Sub SimpleByRef(ByVal x As Integer, ByRef y As Integer)
    176.         Debug.WriteLine("SimpleByRef called, x=" + x.ToString() + ", y= " + (x * 2).ToString())
    177.         y = x * 2
    178.     End Sub
    179.  
    180.     Public Sub SimpleException()
    181.         Throw New NotSupportedException("Exception message")
    182.     End Sub
    VB Code:
    1. 'This can be used to test the above
    2.         'Create the object and run through the methods
    3.         Dim obj As MyDemoClass = New MyDemoClass("test")
    4.  
    5.         obj.SimpleMethod()
    6.  
    7.         obj.SimpleArgs(1)
    8.  
    9.         Debug.WriteLine("Return: " + obj.SimpleReturn(2).ToString())
    10.  
    11.         Dim y As Integer
    12.         obj.SimpleByRef(3, y)
    13.         Debug.WriteLine("ByRef return: " + y.ToString())
    14.  
    15.         'This will throw an exception
    16.         Try
    17.             obj.SimpleException()
    18.         Catch ex As ExecutionEngineException
    19.             '...as expected
    20.         End Try
    21.  
    22.         obj = Nothing

    The objective is to have the object instantiated at the server-side (Singleton) and have the proxy run its logging code also on the server-side. Anyone has any ideas?
    Last edited by ntg; Jun 25th, 2005 at 06:48 PM.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width