0

Hi I have simple client to write value to UA server. I am using OpcLabs libraries in VS2017, Win10 NetFr 4.8. Libraries are loaded from other folder than executable. When dlls are in the same folder both methods below works, when resolver should be triggered = dlls are not in folder - it is not. The problem is simple. Resolver:

private static Assembly OpcAssRes(object sender, ResolveEventArgs args)
    {
        var ProgramFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
        string OpcAsmPath = Path.Combine(ProgramFiles, "OPC Labs QuickOPC 2020.2", "Assemblies", "net47");
        var ReqAss = new AssemblyName(args.Name).Name;
        foreach (string FAssName in Directory.GetFiles(OpcAsmPath, "*.dll"))
        {
            if (Path.GetFileNameWithoutExtension(FAssName) == ReqAss)
            {
                var DLL = Assembly.LoadFrom(FAssName);
                return DLL;
            }
        }
        return null;
    }

And when I want to instantiate the client using the method from libraries:

Works:

   static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyResolve += OpcAssRes;
        ClCon();
    }

    static void ClCon()
    {
        var cl = new EasyUAClient();
    }

Does NOT work - Exception is immediately raised, that the library cannot be found. I tried even sleep it before resolver, but it doesnt - instant Exception- like the resolver isnt triggered. When breakpoint is added in resolver, it does not get to it = it is not started.:

       static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyResolve += OpcAssRes;
        var cl = new EasyUAClient();
    }

Is it normal? Thanks for your advices.

Mimoa
  • 50
  • 4

1 Answers1

1

Types used in a method are loaded before the method is called. If you bind your assembly resolver inside a method that uses a type in an external assembly, the loader won't be bound yet.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272