0

Possible Duplicate:
How do the In and Out attributes work in .NET?

I'm using .Net Remoting (yeah I know... that's old) to implement some service class hosted in a Windows Service.

I call a method DoOperation(Person[] people) on the service to change some property in the Person class just like the code below:

Person p = new Person();
p.SomeProperty = "This value should be changed by the service";

RemoveSvc svc = (RemoveSvc)Activator.GetObject(
    typeof(RemoveSvc),
    "tcp://localhost:6100/RemoteService.rem");

svc.DoOperation(new Person[]{ p });

// This writes the old value not the one changed by the service.
Console.WriteLine(p.SomeProperty); 

The DoOperation method code looks like this:

public void DoOperation(Person[] people) {
    foreach(var o in people)
        p.SomeProperty = "New Value";
}

The problem here is that when the method returns the value of Person.SomeProperty is not updated in the client code.

I have double checked that Person class is serializable;

The svc variable is of type TranparentProxy;

The RemoteSvc class inherits MarshalByRefObject;

I´m using .net Framework 4 (and no, I can´t migrate to WCF)

What am I doing wrong? Is this the expected behavior of .net Remoting? If it is, there is a way to change it?

Tks

Community
  • 1
  • 1
Marcelo de Aguiar
  • 1,432
  • 12
  • 31

2 Answers2

2

That's not how services work. Services are not in-memory operations. When you do what you show with classes you write on your local machine, you naturally see the changes to the Person object because you're passing a memory reference between the caller and the method. Naturally, it's the exact same person object.

However, when you use services or remoting, it isn't the same person any longer. In fact, you state this in your question. You serialize the person, and ship it across the wire. It is not the same person in the same shared memory register. So, changes in the Person on the server will not be seen in the client.

To get what you want, here's your function signature.

public Person[] DoOperation(Person[] people);

You can change the signature to hold onto the reference. Then the object will be kept in sync between the server in client. See this article for more information.

public void DoOperation(ref Person[] people);
Jarrett Meyer
  • 19,333
  • 6
  • 58
  • 52
0

Jarrett is correct, but that may only be part of the solution. The problem is even if you pass a reference to the array, you still have copied the Persons within the array. So changing one of them remotely does not affect the local copy. You may need to reference them through the array rather than using local references.

I would try something like:

var temp = new Person[]{ p }
svc.DoOperation(temp);

// This writes the old value not the one changed by the service.
Console.WriteLine(temp[0].SomeProperty); 

that may use the proxy created by the remoting call to fetch the Person from the remote location rather than using the local reference.

D Stanley
  • 149,601
  • 11
  • 178
  • 240