From the main AppDomain, I am trying to call an async method defined in a type instantiated in a different AppDomain.
For example, the following type MyClass
inherits from MarshalByRefObject
and is instantiated in a new AppDomain :
public class MyClass : MarshalByRefObject
{
public async Task<string> FooAsync()
{
await Task.Delay(1000);
return "Foo";
}
}
In the main AppDomain I create a new AppDomain and create an instance of MyClass inside this AppDomain, then I make a call to the async method.
var appDomain = AppDomain.CreateDomain("MyDomain");
var myClass = (MyClass)appDomain.CreateInstanceAndUnwrap(typeof(MyClass).Assembly.FullName, typeof(MyClass).FullName);
await myClass.FooAsync(); // BAM !
Of course, I get a SerializationException
when I try to make the call, as the Task
type does not inherit from MarshalByRefObject
, nor is serializable.
How could I get arround this ? I would really like to be able to call/await on async methods from a type instantiated in another AppDomain ... Is there a way ?
Thanks !