I am having problems with CreateInstanceAndUnwrap at the moment for some reason (worked previously).
My process is this:
I dynamically generate some code and load the DLL from a subdirectory through MEF. These applications then load different parts (on demand) from these DLLs. I had to update my code to now include the AppDomainSetup, which contains the path to the calling assembly.
I am correctly creating a new AppDomain - no problem. When I try to run this code:
object runtime = domain.CreateInstanceAndUnwrap( typeof(CrossDomainApplication).Assembly.FullName, typeof(CrossDomainApplication).FullName);
I have serious problems - the runtime (variable above) can no longer be applied to CrossDomainApplication or ICrossDomainApplication.
The actual object is as follows:
public class CrossDomainApplication : MarshalByRefObject, ICrossDomainApplication
And the interface looks like this:
public interface ICrossDomainApplication { void Run(CrossDomainApplicationParameters parameters); }
And the parameters look like this:
[Serializable] public class CrossDomainApplicationParameters : MarshalByRefObject { public object FactoryType { get; set; } public Type ApplicationType { get; set; } public string ModuleName { get; set; } public object[] Parameters { get; set; } }
The native type of runtime seems to be MarshalByRefObject - and it doesn't like to convert anything else.
Any thoughts on what might be wrong?
EDIT: Here's the error I get when I run it like the following:
ICrossDomainApplication runtime = (ICrossDomainApplication)domain.CreateInstanceAndUnwrap( typeof(CrossDomainApplication).Assembly.FullName, typeof(CrossDomainApplication).FullName);
System.InvalidCastException: Cannot pass transparent proxy for input "Infrastructure.ICrossDomainApplication".
Here's what the area looks like, how I create it:
AppDomain domain = AppDomain.CreateDomain( Guid.NewGuid().ToString(), null, new AppDomainSetup() { ApplicationBase = GetPath(), ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName, LoaderOptimization = LoaderOptimization.MultiDomainHost });
and GetPath () looks like this:
private string GetPath() { Uri path = new Uri(Assembly.GetCallingAssembly().CodeBase); if (path.IsFile) { path = new Uri(path, Path.GetDirectoryName(path.AbsolutePath)); } return path.LocalPath.Replace("%20", " "); }