title: CreateInstanceAndUnwrap in Another Domain?
body: <p>I'm having issues with CreateInstanceAndUnwrap at the moment for some reason (was working prior).</p>
  
  <p>My process is this:</p>
  
  <p>I dynamically generate some code and it loads DLL's from a subdirectory via MEF.  These applications then load different pieces (on demand) from those DLL's.  I had to update my code to now include an AppDomainSetup that contains the path of the calling assembly.</p>
  
  <p>I create the new AppDomain correctly -- no issues.  When I try to run this code:</p>
  
  <pre><code>object runtime = domain.CreateInstanceAndUnwrap(
                  typeof(CrossDomainApplication).Assembly.FullName,
                  typeof(CrossDomainApplication).FullName);
  </code></pre>
  
  <p>I have massive problems -- the runtime (variable above) no longer can cast to CrossDomainApplication or ICrossDomainApplication.</p>
  
  <p>The actual object looks like:</p>
  
  <pre><code>public class CrossDomainApplication : MarshalByRefObject, ICrossDomainApplication
  </code></pre>
  
  <p>And the interface looks like:</p>
  
  <pre><code>public interface ICrossDomainApplication
  {
      void Run(CrossDomainApplicationParameters parameters);
  }
  </code></pre>
  
  <p>And the parameters look like:</p>
  
  <pre><code>[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; }
  }
  </code></pre>
  
  <p>The native type of runtime appears to be MarshalByRefObject -- and it doesn't like converting to anything else.</p>
  
  <p>Any thoughts on what could be wrong?</p>
  
  <p>EDIT: Here's the error I get when I run it as the following:</p>
  
  <pre><code>            ICrossDomainApplication runtime = (ICrossDomainApplication)domain.CreateInstanceAndUnwrap(
                       typeof(CrossDomainApplication).Assembly.FullName,
                       typeof(CrossDomainApplication).FullName);
  
              //Exception before reaching here
              runtime.Run(parameters);
  </code></pre>
  
  <p>System.InvalidCastException: Unable to cast transparent proxy to type 'Infrastructure.ICrossDomainApplication'.</p>
  
  <p>Here's what the domain looks like, as I create it:</p>
  
  <pre><code>        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
                     });               
  </code></pre>
  
  <p>and GetPath() looks like this:</p>
  
  <pre><code>    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", " ");
      }
  </code></pre>