home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Remoting / ObjectZone / MyObjectLibrary.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  7.2 KB  |  302 lines

  1. using System;
  2. using System.Runtime.Remoting;
  3.  
  4. namespace MyObjectLibrary
  5. {
  6.     // This clas is a Client Activated Object
  7.     public class Baz : MarshalByRefObject
  8.     {
  9.         public String _name;
  10.         public int _totalNumber = 0;
  11.  
  12.         public Baz(String name)
  13.         {
  14.             Console.WriteLine("Baz constructor with args - name: " + name);
  15.             _name = name;
  16.         }
  17.  
  18.         public bool DoWorkWithNumber(int number)
  19.         {
  20.             lock(this)
  21.             {
  22.                 Console.WriteLine("Change: " + number);
  23.                 _totalNumber += number;
  24.                 Console.WriteLine("Total: " + _totalNumber);
  25.  
  26.             }
  27.  
  28.             return true;
  29.         }
  30.  
  31.         public int RetrieveTotalNumber()
  32.         {
  33.             return _totalNumber;
  34.         }
  35.  
  36.         public String RetrieveName()
  37.         {
  38.             return _name;
  39.         }
  40.  
  41.         protected override void Finalize()
  42.         {
  43.             Console.WriteLine("Baz Finalize called");
  44.         }
  45.     }
  46.  
  47.     // General class to demonstrate rich argument types
  48.     public class Foo : MarshalByRefObject
  49.     {
  50.         public Foo()
  51.         {
  52.         }
  53.  
  54.         // fields
  55.         public int intField;
  56.         public double doubleField;
  57.         public String stringField;
  58.         public Object objectField;
  59.  
  60.         // methods
  61.         public void VoidVoid() 
  62.         {
  63.         }
  64.  
  65.         public void VoidInt4(int i)
  66.         {
  67.         }
  68.  
  69.         public void VoidString(String s)
  70.         {
  71.         }
  72.  
  73.         public void VoidByteArray(byte[] b)
  74.         {
  75.         }
  76.  
  77.         public String StringVoid()
  78.         {
  79.             return "String";
  80.         }
  81.  
  82.         public String StringString(String s)
  83.         {
  84.             return s;
  85.         }
  86.  
  87.         public byte[] ByteArrayByteArray(byte[] b)
  88.         {
  89.             return b;
  90.         }
  91.  
  92.         // properties
  93.         private int _intProperty = 0;
  94.         private double _doubleProperty = 0.0;
  95.         private String _stringProperty = "Default";
  96.  
  97.  
  98.         public int IntProperty
  99.         {
  100.             get
  101.             {
  102.                 return _intProperty;
  103.             }
  104.             set
  105.             {
  106.                 _intProperty = value;
  107.             }
  108.         }
  109.  
  110.         public double DoubleProperty
  111.         {
  112.             get
  113.             {
  114.                 return _doubleProperty;
  115.             }
  116.             set
  117.             {
  118.                 _doubleProperty = value;
  119.             }
  120.         }
  121.  
  122.         public String StringProperty
  123.         {
  124.             get
  125.             {
  126.                 return _stringProperty;
  127.             }
  128.             set
  129.             {
  130.                 _stringProperty = value;
  131.             }
  132.         }
  133.  
  134.         // error cases
  135.         public void ThrowsException()
  136.         {
  137.             throw new RemotePerfException("Exception from server");
  138.         }
  139.  
  140.         public void ThrowsException(int n)
  141.         {
  142.             throw new RemotePerfException("Exception from server");
  143.         }
  144.  
  145.         public void ThrowsException(String s)
  146.         {
  147.             throw new RemotePerfException("Exception from server");
  148.         }
  149.  
  150.         public void ThrowsException(Object o)
  151.         {
  152.             throw new RemotePerfException("Exception from server");
  153.         }
  154.  
  155.         public int ThrowsExceptionInt()
  156.         {
  157.             throw new RemotePerfException("Exception from server");
  158.         }
  159.  
  160.         public int ThrowsExceptionInt(int n)
  161.         {
  162.             throw new RemotePerfException("Exception from server");
  163.         }
  164.  
  165.         public int ThrowsExceptionInt(String s)
  166.         {
  167.             throw new RemotePerfException("Exception from server");
  168.         }
  169.  
  170.         public int ThrowsExceptionInt(Object o)
  171.         {
  172.             throw new RemotePerfException("Exception from server");
  173.         }
  174.  
  175.         public String ThrowsExceptionString()
  176.         {
  177.             throw new RemotePerfException("Exception from server");
  178.         }
  179.  
  180.         public String ThrowsExceptionString(int n)
  181.         {
  182.             throw new RemotePerfException("Exception from server");
  183.         }
  184.  
  185.         public String ThrowsExceptionString(String s)
  186.         {
  187.             throw new RemotePerfException("Exception from server");
  188.         }
  189.  
  190.         public Object ThrowsExceptionString(Object o)
  191.         {
  192.             throw new RemotePerfException("Exception from server");
  193.         }
  194.  
  195.         public Object ThrowsExceptionObject()
  196.         {
  197.             throw new RemotePerfException("Exception from server");
  198.         }
  199.  
  200.         public Object ThrowsExceptionObject(int n)
  201.         {
  202.             throw new RemotePerfException("Exception from server");
  203.         }
  204.  
  205.         public Object ThrowsExceptionObject(String s)
  206.         {
  207.             throw new RemotePerfException("Exception from server");
  208.         }
  209.  
  210.         public Object ThrowsExceptionObject(Object o)
  211.         {
  212.             throw new RemotePerfException("Exception from server");
  213.         }
  214.     }
  215.  
  216.  
  217.     //
  218.     // Context Classes that show how to build a Context Attribute, 
  219.     // Property and Message Sink
  220.     //
  221.  
  222.     [AttributeUsage(AttributeTargets.Class)]
  223.     public class IsolationAttribute : 
  224.                 ContextAttribute, 
  225.                 IContributeServerContextSink, 
  226.                 IContributeClientContextSink
  227.     {
  228.         private static String PROPERTY_NAME = "ContextIsolationAttribute";
  229.  
  230.         public IsolationAttribute()
  231.         : base(PROPERTY_NAME)
  232.         {
  233.         }
  234.  
  235.         // Override ContextAttribute's implementation of IContextAttribute.IsContextOK
  236.         public override bool IsContextOK(Context ctx, IConstructionCallMessage msg)
  237.         {
  238.             return base.IsContextOK(ctx, msg);
  239.         }
  240.  
  241.         // Override ContextAttribute's impl. of IContextAttribute::GetPropForNewCtx
  242.         public IContextProperty GetPropertyForNewContext(Object cookie)
  243.         {
  244.             return this;
  245.         }
  246.  
  247.  
  248.         public IMessageSink GetServerContextSink(IMessageSink nextSink)
  249.         {
  250.             return new IsolationSink(this, nextSink);
  251.         }
  252.  
  253.         public IMessageSink GetClientContextSink(IMessageSink nextSink)
  254.         {
  255.             return new IsolationSink(this, nextSink);
  256.         }
  257.     }
  258.  
  259.     public class IsolationSink : IMessageSink
  260.     {
  261.         private IMessageSink _nextSink;
  262.         private IsolationAttribute _prop;
  263.  
  264.         public IsolationSink(IsolationAttribute prop, IMessageSink nextSink)
  265.         {
  266.             _prop = prop;
  267.             _nextSink = nextSink;
  268.         }
  269.  
  270.         public IMessage SyncProcessMessage(IMessage reqMsg)
  271.         {
  272.             return _nextSink.SyncProcessMessage(reqMsg);
  273.         }
  274.  
  275.         public IMessageCtrl AsyncProcessMessage(IMessage reqMsg, IMessageSink replySink)
  276.         {
  277.             return _nextSink.AsyncProcessMessage(reqMsg, replySink);
  278.         }
  279.  
  280.         public IMessageSink NextSink
  281.         {
  282.             get
  283.             {
  284.                 return _nextSink;
  285.             }
  286.         }
  287.  
  288.     }
  289.  
  290.     public class RemotePerfException : Exception
  291.     {
  292.         public RemotePerfException(String s) : base(s){}
  293.     }
  294.  
  295.  
  296.     [IsolationAttribute()]
  297.     class ContextBoundClassWithIsolationAttribute : ContextBoundObject
  298.     {
  299.  
  300.     }
  301. }
  302.