views:

34

answers:

1

Hi

I have the following procedure

private static IMyInterface OpenInstance( 
string assemblyPath, 
string classType, 
string assemblyName, 
out AppDomain domainInstall)
{
   IMyInterface interface = null;

   AppDomainSetup domaininfo = new AppDomainSetup();

   domaininfo.ApplicationBase = assemblyPath;
   domainInstall = AppDomain.CreateDomain("PathInstall", null, domaininfo);

   ObjectHandle handleService = null;
   try
   {

      handleService = Activator.CreateInstance(
      domainInstall,
      assemblyName,
      classType,
      true,
      System.Reflection.BindingFlags.CreateInstance,
      null,
      new Object[] { assemblyName},
      System.Globalization.CultureInfo.CurrentCulture,
      null, null);

      Object myobject = handleService.Unwrap();
      interface = (IMyInterface )myobject ;
    }
    catch (Exception ex)
    {
       ...
    }

    return interface ;
}

This procedure works ever fine, but when it is called during an installation custom action.

In other words if i call my OpenInstance(...) procedure inside my own Install(...) override:

public override void Install(IDictionary stateServer)

Defined in my Installer extended class:

[RunInstaller(true)]
public class SpheresServiceInstaller : Installer

I got an exception when i try to cast my unwrapped object to the desired type:

interface = (IMyInterface)myobject ;

Exception details:

  • Type: System.InvalidCastException
  • Message: Unable to cast transparent proxy to type 'IMyInterface'.

I would like to understand why the procedure works ever but in this specific case.

Details

  • I followed step by step the object creation procedure and everything seems fine, the object is well created by the Activator.CreateInstance procedure.

  • The assembly, which is used by the Activator.CreateInstance, already exists on the file system.

  • The specific assembly 'assemblyName on the source code) is a window service that has been just created by the installation procedure.

A: 

I solved the problem following the link proposed at this post

stack overflow: appdomain-createinstancefromandunwrap-unable-to-cast-transparent-proxy

The link that gives us the solution code is

west-wind.com: Assembly Loading across appdomain

It is really a basic thing, i was falled in the case where an assembly is loaded by an external application (in my specific case: the wow64 installer application).

The application does not know where to find the assemblies that depend from the main assembly you are loading, so you have to write a custom assembly resolver for the current application domain (in my specific case: the wow64 installer application) in order to give the necessary loading information inside it.

Head to the west-winf link to get the code, it works perfectly

Marcello Faga