Found a bug in Remoting / Serialization

  • Thread starter Thread starter Howard Swope
  • Start date Start date
H

Howard Swope

I came across an interesting bug I thought I would throw out there. It
occurs when you are remoting a parameter that is of the same type of the
class that is being called for example the first line of the Copy routine
succeeds where the second fails:

class MyType() : MarshalByRefObject
{
private string name;

public MyType()
{
}

public Copy(MyType myType)
{
// succeeds
this.name = myType.Name;

// throws exception if the myType is a remoting proxy
this.name = myType.name;
}

public string Name
{
get
{
return name;
}
}
}
 
It's not that the method has a 'MyType' parameter:

You cannot access fields of a remoted object. You have to add a set
accessor to the Name property and use the property, not the field, in
the Copy method.

Remoting, IMO, must be used very carefully. There are a lot of gotchas
which can occur.
 
Following works for me, what exception is thrown on you?

// rdomas.cs
using System;
using System.Reflection;
namespace Willys {
public class HelloWorldWorker : MarshalByRefObject
{
string s;
public string S
{
get { return s;}
}
public void SayHello(HelloWorldWorker w)
{
this.s = "Hello";
this.s = w.S;
this.s = w.s + " world";
}
}
}

// main.cs
....
public static void Main()
{
AppDomain newDomain = AppDomain.CreateDomain("Second AppDomain");
HelloWorldWorker remote =
(HelloWorldWorker)newDomain.CreateInstanceAndUnwrap("rdomas",
"Willys.HelloWorldWorker");
remote.SayHello(remote);
Console.WriteLine(remote.S);
}

Willy.
 
I don't see that in the documentation, but I'll take your word for it. I
wonder why this is so. Obviously the data is there because I can access it
through the property. I guess it is the way that the proxy is wrapping
things up that makes this difficult or is there some other reason that
having direct access to the fields is unwise?
 
Hi,
I have a doubt. myType is an instance of class MyType and name is a private
variable within MyType. How can myType.name work?? It has to throw an
exception coz name is private??
Excuse me if I sound absurd.
 
myType.Name is being called from within a method of MyType. So it should
work and it does from anything but a Remoting proxy. I have been told in
another post that you can't access any fields from Remoting proxy. I
actually swithed MyType over to be Marshal by Value and it is behaving
properly.
 
I have a sample code it didn't work for me.

remote object code:

public class Hello:MarshalByRefObject
{
private ProductInfo pf;

public ProductInfo Product
{
get {return pf; }
}

public Hello()
{
System.Console.WriteLine("Constructor");
}

public string PrintHello()
{
System.Console.WriteLine("PrintHello");
pf = new ProductInfo();
pf.ProductID = 1;
pf.ProductXML="<Test>Remoting</Test>";
}
}



[Serializable]
public class ProductInfo //: MarshalByRefObject
{
public Int32 ProductID;
public string ProductXML;
public ArrayList Arr;

public ProductInfo()
{
Arr = new ArrayList();
Arr.Add(new Arr1(1));
}

}//class



client code: [getting exception at 4th line ]
ProductInfo pf;
b1 = h.PrintHello();
pf = h.Product;
this.textBox1.Text = pf.ProductID.ToString();


any clue why getting error.
 
You cannot access fields of a remoted object. You have to add a set
accessor to the Name property and use the property, not the field, in
the Copy method.

I am far from a remoting expert, but I do not believe this is right. I
read (somewhere) in the dox that remote fields are automatically
turned into properties on the proxy.

Naturally, now I can't find an example in any of my remoting code ....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top