lundi 21 août 2017

System.Runtime.Remoting.RemotingException invoking method of an external assembly

I'm playing with reflection and remoting for the first time. I have an assembly present on the machine, that I have read-only access to the source code of, and I want to invoke one of its methods directly. This is my code:

        AppDomain NewDomain = AppDomain.CreateDomain("NewDomain");
        try
        {
            var objectHandle = NewDomain.CreateInstanceFrom(@"C:\Program Files (x86)\xxx\ExistingAssembly.dll", "RemoteNamespace.RemoteClass");
            var localObject = (RemoteNamespace.ISharedInterface)objectHandle.Unwrap();
            File.Copy(localObject.StrTargetMethod(arg1),
                Path.Combine(logDir, logfile), true);
        }
        finally
        {
            AppDomain.Unload(NewDomain);
        }

At the first line of the try block, the CreateInstanceFrom method throws an UnhandledException: System.Runtime.Remoting.RemotingException: Cannot load type 'RemoteNamespace.RemoteClass, RemoteNamespace, Version=1.2.0.8, Culture=neutral, PublicKeyToken=xxxxxxxxxx'.

EDIT: I'm now trying this:

var assembly = Assembly.LoadFrom(@C:\Program Files (x86)\xxx\ExistingAssembly.dll");
var localObject = (ISharedInterface)assembly.CreateInstance("RemoteNamespace.RemoteClass");

but I'm getting the same error being thrown at assembly.CreateInstance().





Aucun commentaire:

Enregistrer un commentaire