I'm studying this code from MSDN MarshalByRefObject I couldn't figure out why this is throwing an exception. I'm thinking if it could be the Worker type is in the same assembly as the class Program? Do I need to install this assembly in the GAC, though I'm running this in debug mode in Visual Studio 2017 editor. I don't have a solid understanding of how to work with assembly.
using System;
using System.Reflection;
namespace AppDomainMarshalByRefObject
{
public class Program
{
public static void Main(string[] args)
{
Worker localWorker = new Worker();
localWorker.PrintDomain();
AppDomain ad = AppDomain.CreateDomain("New domain");
Worker remoteWokrer = (Worker)ad.CreateInstanceAndUnwrap(typeof(Worker).Assembly.FullName, "Worker");
remoteWokrer.PrintDomain();
Console.ReadKey();
}
}
public class Worker : MarshalByRefObject
{
public void PrintDomain()
{
Console.WriteLine("Object is executing in AppDomain \"{0}\"",
AppDomain.CurrentDomain.FriendlyName);
}
}
}
You instantiate an incomplete typename
. Add this to your code:
public static void Main(string[] args)
{
...
var typeName = typeof(Worker);
AppDomain ad = AppDomain.CreateDomain("New domain");
Worker remoteWokrer = (Worker)ad.CreateInstanceAndUnwrap(typeof(Worker).Assembly.FullName, typeName.FullName);
...
}