0

I have created a new domain, then loaded the assembly into this domain, but when GetTypes() gives an error like the picture attached, hope everyone helps, thanks. Code

public class Program
{       
    public static void Main()
    {
        string assemblyPath = @"D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug\BeyConsRevitProject.dll";
        AppDomain appDomain = CreateChildDomain(AppDomain.CurrentDomain, Guid.NewGuid().ToString());
        appDomain.AssemblyResolve += AssemblyResolve;
        var value = (Proxy)appDomain.CreateInstanceAndUnwrap(typeof(Proxy).Assembly.FullName, typeof(Proxy).FullName);
        var assembly = value.GetAssembly(assemblyPath);
        var types = assembly.GetTypes();
        Console.ReadKey();
    }

    private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
    {
        AssemblyName assemblyName = new AssemblyName(args.Name);
        string dependentAssemblyFilename = Path.Combine(@"D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug", assemblyName.Name + ".dll");
        if (File.Exists(dependentAssemblyFilename)) return null;
        return Assembly.LoadFile(dependentAssemblyFilename);
    }

    public static AppDomain CreateChildDomain(AppDomain parentDomain, string domainName)
    {
        Evidence evidence = new Evidence(parentDomain.Evidence);
        AppDomainSetup setup = parentDomain.SetupInformation;
        return AppDomain.CreateDomain(domainName, evidence, setup);
    }
}

public class Proxy : MarshalByRefObject
{
    public Assembly GetAssembly(string assemblyPath)
    {
        try
        {
            return Assembly.LoadFile(assemblyPath);
        }
        catch { return null; }
    }
}

Error

enter image description here

enter image description here

NhuTruong
  • 11
  • 5
  • Why do you keep posting essentially the same question? As I mentioned in your [original](https://stackoverflow.com/q/64724011/585968) question, that [Codeproject](https://www.codeproject.com/Articles/453778/Loading-Assemblies-from-Anywhere-into-a-New-AppDom#_articleTop) code you are using has multiple issues. Also I see you are not using the resolve paths I suggested in my [answer](https://stackoverflow.com/a/64724104/585968), the one you said leaves your questions _"unsolved"_ –  Nov 09 '20 at 00:47

1 Answers1

0

Have you checked if BeyConsRevitProject.dll assembly is in the bin directory of your application? This is a possible cause. Try deleting the bin/ and obj/ folders and rebuilding your solution, if the error persists, use this code below to ascertain the real reason for the error:

using System.IO;
using System.Reflection;
using System.Text;

try
{
    //The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
    StringBuilder sb = new StringBuilder();
    foreach (Exception exSub in ex.LoaderExceptions)
    {
        sb.AppendLine(exSub.Message);
        FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
        if (exFileNotFound != null)
        {                
            if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
            {
                sb.AppendLine("Fusion Log:");
                sb.AppendLine(exFileNotFound.FusionLog);
            }
        }
        sb.AppendLine();
    }
    string errorMessage = sb.ToString();
    //Display or log the error based on your application.
}

This code was suggested by Ben Gripka here: Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'

Pedro Coelho
  • 1,411
  • 3
  • 18
  • 31
  • I deleted the bin directory and rebuilt it, using try-catch as you instructed, but it does not jump inside the catch. Image below – NhuTruong Nov 07 '20 at 14:50
  • That is because a different exception is being thrown now and this one is not from type ReflectionTypeLoadException. But by this new exception, we could confirm that the assembly really has/had a missing reference. Check for BeyConsRevitProject on your project's dependencies and write its correct path or add it as dependency if it is not referenced anywhere in your project – Pedro Coelho Nov 07 '20 at 14:58
  • My project's .csproj: https://docs.google.com/document/d/1MaP32T634kUAVzgXrrijRlcoAQn40jKCyVsCqxc8LcY/edit?usp=sharing – NhuTruong Nov 07 '20 at 15:08
  • Is this dll really here in this path in your local machine? D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug\BeyConsRevitProject.dll – Pedro Coelho Nov 07 '20 at 15:25
  • Yes, all dll are there D:\Github\BeyConsPlugin\BeyConsProject\bin\x64\Debug – NhuTruong Nov 07 '20 at 15:28
  • I have a similar error in this post https://stackoverflow.com/questions/64724011/cannot-get-types-from-assembly-after-reflectiononlyloadfrom – NhuTruong Nov 07 '20 at 15:31
  • Currently, I made a lot of ways that don't resolve, so sad – NhuTruong Nov 07 '20 at 15:32
  • Why dont you simply add this dll as reference in "Add Reference" and then load it from its path? It may be easier. Check here how to add the dll as reference: https://stackoverflow.com/questions/12992286/how-to-add-a-dll-reference-to-a-project-in-visual-studio/12992312 – Pedro Coelho Nov 07 '20 at 15:40
  • Note that, it would only work for debug mode, for production you will need to add it as an artifact – Pedro Coelho Nov 07 '20 at 15:40
  • I use the load / unload assembly method because the dll changes, updating continuously – NhuTruong Nov 07 '20 at 15:51