Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 22-29 extracted from chapter .NET Remoting


Listing 22-28<     > Listing 22-30


This listing can be compiled with the command line:
csc.exe /target:exe Example_22_29.cs
Errors: 0 Warnings: 0


Example_22_29.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Activation;

public class CustomRealProxy : RealProxy {
   readonly bool m_bDisplay;
   readonly MarshalByRefObject m_TargetObj;

   public CustomRealProxyMarshalByRefObject targetObj,
                           Type type, bool bDisplay ) : base(type) {
      m_bDisplay = bDisplay;
      m_TargetObj = targetObj;
   }

   public override IMessage Invoke( IMessage msgIn ) {
      IMessage msgOut;
      if ( msgIn is IConstructionCallMessage ) {
         IConstructionCallMessage ctorCallMsg =
             (IConstructionCallMessage) msgIn;

         // Get the default real proxy.
         RealProxy defaultRealProxy =
            RemotingServices.GetRealProxy( m_TargetObj );

         // Invoke the ctor on this real proxy
         defaultRealProxy.InitializeServerObject( ctorCallMsg );

         // Get the custom real proxy on the new object.
         msgOut=EnterpriseServicesHelper.CreateConstructionReturnMessage(
            ctorCallMsg, (MarshalByRefObject)GetTransparentProxy() );

         if ( m_bDisplay )
            Console.WriteLine( "CustomRealProxy: ctor call" );
      } 
      else {
         IMethodCallMessage callMsg = (IMethodCallMessage) msgIn;

         if ( m_bDisplay )
            Console.WriteLine( "CustomRealProxy: Before calling:{0}",
                               callMsg.MethodName);

         msgOut = RemotingServices.ExecuteMessage( m_TargetObj, callMsg );

         if ( m_bDisplay )
            Console.WriteLine( "CustomRealProxy: After calling:{0}",
                               callMsg.MethodName);
      }
      return msgOut;
   }
}

[AttributeUsage(AttributeTargets.Class)]
public class CustomProxyAttribute : ProxyAttribute {
   bool m_bDisplay;
   public CustomProxyAttributebool bDisplay ) {
      m_bDisplay = bDisplay;
   }
   public override MarshalByRefObject CreateInstance( Type T ) {
      MarshalByRefObject targetObj = base.CreateInstance( T );
      RealProxy realProxy = new CustomRealProxy(targetObj, T, m_bDisplay);
      return (MarshalByRefObject) realProxy.GetTransparentProxy();
   }
}

// The true params indicates that we wish that the custom real
// proxy displays info on the console.
[CustomProxyAttribute(true)]
public class Adder : ContextBoundObject {
   public int Add( int a, int b ) { return a + b; }
}

public class Program {
   static void Main() {
      Adder obj = new Adder();
      obj.Add(56);
   }
}	
Copyright Patrick Smacchia 2006 2007