The following sample is based on "Passing values back and forth appdomains", where Marc Gravell kindly provided a very good answer to a question about .Net remoting between appdomains. What I've done is extended it in a (very naive?) expectation that it should also work for an array of strings.
The problem is that it only works one way - the created appdomain can access the array, but only readonly. What I'd like is to get the updated array elements back in the original appdomain too. I'd even like to do this with List<> and Dictionary<> objects. Is this possible?
using System;
namespace StackoverflowSample
{
class MyBoundaryObject : MarshalByRefObject
{
public void SomeMethod(AppDomainArgs ada)
{
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + "; executing");
ada.MyString = "working!";
ada.MyStringArray[0] = "working!";
string s = ada.MyStringArray[0]; // s is assigned value "a"!!!
}
}
public class AppDomainArgs : MarshalByRefObject
{
public string MyString { get; set; }
public string[] MyStringArray { get; set; }
}
static class Program
{
static void Main()
{
AppDomain domain = AppDomain.CreateDomain("Domain666");
MyBoundaryObject boundary = (MyBoundaryObject)
domain.CreateInstanceAndUnwrap(
typeof(MyBoundaryObject).Assembly.FullName,
typeof(MyBoundaryObject).FullName);
AppDomainArgs ada = new AppDomainArgs();
ada.MyString = "abc";
ada.MyStringArray = new string[] { "a", "b" };
Console.WriteLine("Before: " + ada.MyString + " " + ada.MyStringArray[0]);
boundary.SomeMethod(ada);
Console.WriteLine("After: " + ada.MyString + " " + ada.MyStringArray[0]);
Console.ReadKey();
AppDomain.Unload(domain);
}
}
}
Sometime back i have this requirement too of returning the updated list back to Main AppDomain and i solved it using a workaround of creating a
new instance of a List
andassign the desired values
. This should work for you -UPDATE
I guess you have to
clone
the instance and instantiate anew instance
before returning from remote method. Reasons for the which it isworking
for simplestring
is -Strings are
immutable
i.e. every time you initialize it with different value, anew instance
is created for it behind the scenes somewhat likenew String()
. Hence, the update is visible in other appDomain.I tried this small thing with
StringBuilder
which aremutable
i.e new instance is not created for them when you change the content of the object.Now, see the output -
You will see that
StringBuilder
object is unchanged. Ideally, its value should be"a working!"
but still the value is"a"
.