InvalidCastException of a Activator.CreateInstance object during an installation procedure

送分小仙女□ 提交于 2019-12-20 04:05:26

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/3116694/invalidcastexception-of-a-activator-createinstance-object-during-an-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!