Problem adding elements to Remote Objects nested ArrayList

Hello,

I'm having trouble with an ArrayList which is nested in within a remote singleton object in .NET Remoting (via IPC). Code first:

Code Snippet

// client.cs

using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;


public static class Client{
public static void Main(){
MyTestObject obj = (MyTestObject) Activator.GetObject(
typeof(MyTestObject),
"ipc://test/Test");
obj.NestedString = "MyText";
Console.WriteLine("NestedString is: " + obj.NestedString);

obj.NestedArrayList.Add("some string");
obj.NestedArrayList.Add("some string");
obj.NestedArrayList.Add("some string");

// this always outputs 0 - so the ArrayList is empty?
Console.WriteLine("NestedArrayList Count is: " + obj.NestedArrayList.Count);

}
}


Code Snippet

// file server.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;

public static class Server {
public static void Main(){
// register a new IPC Channel according to first cmdline argument
IpcChannel serverChannel = new IpcChannel("test");
ChannelServices.RegisterChannel(serverChannel, false);

// register a wellknown object to the channel so it can be cal
led remotely
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(MyTestObject),
"Test",
WellKnownObjectMode.Singleton);
// keep server running until keypress
Console.WriteLine("Server is listening, press a key to stop");
Console.ReadLine();
}
}


Code Snippet

using System.Collections;
using System;
using System.Runtime.Remoting;

public class MyTestObject : MarshalByRefObject {
public ArrayList NestedArrayList;
public string NestedString;

public MyTestObject(){
this.NestedArrayList = new ArrayList();
}
}


When running the client application it seems the .Add() Method of the ArrayList does not do anything and count is always 0. Why is this and how could I achive the behaviour that I expected (the elemets get correctly added and can be pulled again on the client side).

Any help greatly appreciated

[2977 byte] By [McFront] at [2008-1-8]
# 1

do you get an exception or something?
MarlonGrech at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 2
No, i don't get any exceptions, thats the weird thing. But i found out sth: I created a custom Class MyArrayList which inherits from ArrayList and only overrides the Add and Ctor from ArrayList to display an output message when Add() is called:

public class MyArrayList : ArrayList
{
public override int Add(object obj){
Console.WriteLine("Add called");
return base.Add(obj);
}
public MyArrayList():base(){
}

(I'm sorry, neither tab indent nor 'Mark as Codeblock' work in my opera browser)

What happens now is, that upon calling MyArrayList.Add from the Client it displays the output message on the CLIENT windows, instead as expected from me on the SERVER window, which indicates that the code is run on the client though the MyArrayList object is a nested member of the Remote (Singleton) Object. I've no Idea why this happens and am thankfull for any clue how i can get the .Add to be executed on the Server side and the added object is added to the _remote_ Objects ArrayList.

Still stuck with this, any help greatly appreciated.

McFront at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 3

Is you main object inheriting from MarshalByRef?

can you please post me your code....

MarlonGrech at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 4
I use exactly the Code i posted in my first post, it is fully compilable and divided in the three parts:
server.cs: Registers the Wellknown Object and waits for Connections from Clients
client.cs: The client connecting to the server and doing the ArrayList.Add() stuff
shared.cs: The Definition of the Remote Object, which inherits from MarshalByRefObject and is used by both, client and server.

The inherited MyArrayList which i described in me 2nd post is not that important, it justed helped me to find out that ArrayList.Add() is _executed_ on the Client, not on the server. I found out that this issue always exists with nested Classes of any (reference) types and could be described more generic:
i.e.: I got a Remote (Singleton) Object which inherits from MarshalByRefObject, and within it a (non-primitive) reference Type nested Class, like ArrayList for example. If the Client now connects to the Singleton Object via Activator.GetObject(), and tries to Set a MemberValue, Property, call a method. of this NESTED object, it is only done locally without affecting the remote object.

Does the nested Class of the Remote Object have to fullfil some requirements, like inheriting from MarshalByRefObject, too?

McFront at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 5

as far as I know whenever you make a call to the remote object (even access a property) this will be done on the remote object and never locally... I can't understand how this is not working on your machine.... what about configuration? do you have any configuration set up?
MarlonGrech at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 6
Yes, Methods and Properties of the RemoteObject would be executed on the remote Server, but I think the problem here is that ArrayList.Add() is not itself a method of remote object, insted the ArrayList is a member. If i add a wrapper method as direct member to the remoteobject it works:

public class MyTestObject{
[...]
public int AddWrapper(object obj){
return this.NestedArrayList.Add(obj);
}
}

Calling AddWrapper() the object ist correctly inserted in the server-side instance of the arraylist and it works as i expected. Though this is not a very "clean" solution. I still dont know why calling the .Add of the ArrayList instance directly does not work, nor even throw an exception if this is not intended to work.

McFront at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 7

Remoting needs an interface to implemented by Server. Where had you mention that...

Regards,

Wasif Ahmad

ComputerAngel at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 8
Computer Angel wrote:

Remoting needs an interface to implemented by Server.

No, it doesn't. If you stick with default binary serialization you don't need to implement a specific interface, except when you need custom sinks or serialization or such. For simple Singleton SAO you only need to derive your class from MarshalByRefObject.

McFront at 2007-10-2 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...

.NET Development

Site Classified