.NET Remoting, why the list is not deleted?

I use RemotingServices.Marshal and Activator.GetObject to establish a remote channel between two simple programs located on the same computer.

 public class IpcInterface : MarshalByRefObject { public int number = -1; public string text = "default"; public List<String> strings; } // A simplification 

I confirmed that the channel exists, and communication is possible because both programs successfully change number and text to completely unique values ​​(confirmed).

So I immediately tried to do the same for strings .

In one program, I called strings.Add("1") . I tried reading the contents of strings in a second program. He was empty. What else, the score was 0 . I have no idea why strings continues to have 0 objects, as if I never added them. The same thing happens for Stack<T> and Dictionary<T, K> , I just can’t add any elements to it. To be sure that in the general case there wasn’t anything strange with reference types, I also tried putting StringBuilder in the IPC interface class and that the “state” is successfully maintained in both programs by changing its value.

Question: Why is the list of strings not added and what is the solution?

I hope that someone with experience will immediately find this problem. I tried Googling for similar questions, but I did not get any useful results. Surprisingly, I only have 1 good link for googling "debugging .net removes the transparent proxy". This is the second question I have. How can I debug a transparent proxy object?

  • All objects are created correctly (there is no NullReferenceException, in fact there are no exceptions).
+4
source share
1 answer

The problem is that the List<T> itself is not a MarshalByRefObject , but rather a serializable class. When you call the Add() method on your list, what you are actually doing is asking the remote object to serialize its list, deserializing it locally, and then calling the method on the local object. Your changes will never be transferred back to the remote instance of the list object.

You will need to provide methods in your IpcInterface class to manage the list; because this type inherits from MarshalByRefObject , methods will be called on the remote object instead of the locally deserialized instance.

i.e.

 public class IpcInterface : MarshalByRefObject { public List<String> strings; public void Add(string value) { strings.Add(value); } } 

You can also open strings as a read-only collection, otherwise you will create the impression that it can be manipulated directly (which cannot).

+8
source

Source: https://habr.com/ru/post/1447397/


All Articles