Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling referenced .NET assemblies: Could not load file or assembly

See original GitHub issue

Using an DllExported-dll as a broker between VBA and other .NET code (separate dll’s) doesn’t seem to work.

How to reproduce

See attached project

  • ClassLibrary1 exposes UnmanagedExport.CreateBroker(). Works fine.
  • CreateBroker returns new Class1 (from ClassLibrary1). Works fine.
  • Class1.DoNothingOnClass2() creates an instance of Class2 (from ClassLibrary2) and runs a quite simple method on it.

Build de project. In a VBA-module (I used Access):

Declare Function CreateBroker Lib "C:\source\repos\DllExportTest\ClassLibrary1\bin\Debug\ClassLibrary1.dll" () As Object

Public Sub RunTest()
  Dim class1 As Object
  Set class1 = CreateBroker()
  Set class1 = Nothing
End Sub

(be sure to correct the path in the Declare Function to point to the built dll)

Execute the RunTest()

VBA gives “'Could not load file or assembly ‘ClassLibrary2, version=, Culture=neutral, PublicKeyToken=null’ or one of iets dependencies. The system cannot find the file specified.”

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

bertstomphorstcommented, Oct 12, 2018

Thanx Denis, got it work with following change: In Class1.DoNothingOnClass2 (where Class2 was initialized), change body of DoNothingOnClass2() from

        public void DoNothingOnClass2()
            new Class2().DoNothing();


        public void DoNothingOnClass2()
            string path = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
            var assemblyname = "ClassLibrary2";
            var typename = "Class2";

            dynamic class2 = AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap($@"{path}\{assemblyname}.dll", $"{assemblyname}.{typename}");

After that, found that AssemblyResolve works better, because it loads all types from referenced assembly, and in ClassLibrary1 everything is typed. Solution (for others reference): static class UnmanagedExport (from sample project) should be:

    static class UnmanagedExport
        static UnmanagedExport()
            => AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            => Assembly.LoadFrom($@"{new FileInfo(args.RequestingAssembly.Location).DirectoryName}\{args.Name.Split(',')[0]}.dll");

        [return: MarshalAs(UnmanagedType.IDispatch)]
        static object CreateBroker()
            => new Class1();
bertstomphorstcommented, Oct 12, 2018

Thank you so far Denis, I will dive into it and share my experience here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not load file or assembly or one of its dependencies
NET assembly (a .dll or .exe file) to get a graph of all the referenced assemblies while highlighting conflicting or missing references.
Read more >
How to resolve “Could not load file or assembly … or one of its ...
Code base or Probing: First checks in location in <codeBase> element. If not present, then probing is done in Application base considering culture,...
Read more >
Could not load file or assembly
In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were...
Read more >
UnitTest fails for a DLL which uses assemblies (Could not ...
NET) => I'm currently getting “false” test results (I know this library ... FileNotFoundException : Could not load file or assembly 'System.
Read more >
Understanding How Assemblies Load in C# .NET
Determine the version of the assembly that needs to load according to configuration files (app.config or web.config). That configuration file ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found