Re: Remoting Problem

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: shawn (huangsy_at_stpass.com)
Date: 06/17/04


Date: Thu, 17 Jun 2004 16:38:07 +0800

well, the ~RemotingObject() function is not called even when i force a gc.
but in SAO manner, it do get called.

"Serge Lobko-Lobanovsky" <SergeLobkoLobanovsky@discussions.microsoft.com> д
ÈëÓʼþ news:3EDF2F40-9D3E-4D7E-9A51-4FF88875E3BA@microsoft.com...
> That's because there is no such thing called "destructor" in .NET world,
because memory blocks are actually freed by the garbage collector. There are
finalizers, however, which get called just before the GC destroys your
object.
>
> "shawn" wrote:
>
> > I've written a simple application to test the remoting scenario, and
find
> > out that the client activated object's destructor was never called until
> > server application was down.
> > the application is simple.
> >
> > The remoting object
> >
> > public class RemotingObject:MarshalByRefObject
> > {
> > private string _id;
> > public RemotingObject()
> > {
> > Console.WriteLine("Someone Called me");
> > }
> > ~RemotingObject()
> > {
> > Console.WriteLine("Someone killed " + _id);
> > }
> >
> > public string ID
> > {
> > get
> > {
> > return _id;
> > }
> > set
> > {
> > _id = value;
> > }
> > }
> > }
> >
> > the server app
> >
> > class Class1
> > {
> > /// <summary>
> > /// /// </summary>
> > [STAThread]
> > static void Main(string[] args)
> > {
> > TcpServerChannel tsc = new TcpServerChannel(8228);
> > ChannelServices.RegisterChannel(tsc);
> >
> >
RemotingConfiguration.RegisterActivatedServiceType(typeof(RemotingObject.Rem
> > otingObject));
> > Console.WriteLine("");
> >
> > String keyState = "";
> > while (String.Compare(keyState,"0") != 0)
> > {
> > Console.WriteLine("Press a key and ENTER: G=GC.Collect, 0=Exit");
> > keyState = Console.ReadLine();
> >
> > Console.WriteLine("Pressed: " + keyState);
> >
> > // Force a GC
> > if (String.Compare(keyState,"G") == 0)
> > {
> > Console.WriteLine("GC Collect - start");
> > GC.Collect();
> > GC.WaitForPendingFinalizers();
> > Console.WriteLine("GC Collect - done");
> > }
> > }
> > }
> > }
> >
> > and the client
> >
> > [STAThread]
> > static void Main(string[] args)
> > {
> > TcpChannel chan = new TcpChannel();
> >
> > ChannelServices.RegisterChannel(chan);
> >
> >
System.Runtime.Remoting.RemotingConfiguration.RegisterActivatedClientType(ty
> > peof(RemotingObject.RemotingObject),"tcp://localhost:8228");
> >
> > object[] attrs = {new
> >
System.Runtime.Remoting.Activation.UrlAttribute("tcp://localhost:8228/Remoti
> > ngObject")};
> > System.Runtime.Remoting.ObjectHandle handle =
> >
Activator.CreateInstance("RemotingObject","RemotingObject.RemotingObject",at
> > trs);
> >
> > if (handle == null)
> > {
> > MessageBox.Show("Failed");
> > return;
> > }
> > RemotingObject.RemotingObject _ro =
> > (RemotingObject.RemotingObject)handle.Unwrap();
> > _ro.ID="asdqw";
> > Console.WriteLine(_ro.ID);
> > }
> >
> > did i miss something.
> > Thanks in advance.
> >
> > shawn
> >
> >
> >