Load the assembly into a temporary Appdomain from another directory

As part of the BaaS project, I need to dynamically execute code from the compilation. I tried all the examples and answers on other similar questions, including the AppDomainToolKit approach, but no luck.

Similar to the plugin approach, I got a positive result by copying the related assemblies to the path to the host basket. However, this is not possible for the current scenario:

  • Permissions and restrictions should apply to the request
  • every request should be evaluated in a temporary appdomain
  • load necessary assemblies and reference types from a path to a temporary domain

So far my last code snippet is below

        // ClassLibrary1.dll and ClassLibrary2.dll are in the same directory, both are marked as Serializable

        var binPath =  @"C:\AssemblyDemo\ClassLibrary1\bin\Debug\";
        var lib1Path = binPath + "ClassLibrary1.dll";
        var lib2Path = binPath + "ClassLibrary2.dll";

        var setup = new AppDomainSetup();
        setup.ApplicationBase = binPath;
        AppDomain domain = AppDomain.CreateDomain("domainname", null, setup);
        ObjectHandle handle = domain.CreateInstanceFrom(lib1Path, "ClassLibrary1.Class1");
        var unwrap = handle.Unwrap();
        var m1 = unwrap.GetType().GetMethod("Method1");
        var result = m1.Invoke(unwrap, null);

handle.Unwrap() throws an exception Type is not resolved for member 'ClassLibrary1.Class1,ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

+4
source share
1 answer

AppDomain, Unwrap . Unwrap . AppDomain. AppDomain.DoCallBack MarshalByRef, .

+2

Source: https://habr.com/ru/post/1542451/


All Articles