How to debug “Could not load file or assembly” run

2019-01-12 02:23发布

问题:

I have a project that uses a Java library converted using IKVM. I added the created DLL plus all possible IKVM DLLs as references to my project, but when I run it, I get the following runtime error:

System.IO.FileNotFoundException : Could not load file or assembly 'core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

I'm not really sure how to debug this error. Is there a way to know exactly which type is missing? From the description I'd guess this is the generated DLL (from the Java lib) but I have properly added it as reference.

What else have I done wrong?

回答1:

You can use the Fusion Log Viewer to debug assembly loading problems in .NET apps.

Also, Process Monitor is very useful in identifying general file-load problems.



回答2:

Just chiming in that dependency walker and fusion log viewer dont work well for applications that have native and managed code together or doing dynamic loading of native code. Here is a good post explaining step by step how to solve missing (or invalid permission) assembly errors using process monitor that covers those scenarios:

Debug Could not load file or assembly or one of its dependencies error with Process Monitor

The post also includes a tool to automate some of this task as well



回答3:

You can diagnose that by using Fusion Log Viewer (available in the Microsoft SDK). Launch it in Administrator and activate the log in the Settings.

It will log all the informations regarding your references loading (and all their references). It will explicitly tell you which reference is missing and where it has searched for it.

MSDN on Fusion Log Viewer



回答4:

There is a program called Dependency Walker which allows you to see the dependencies of a given PE file (dll, exe, ocx...).

This error is really annoying, very difficult to debug. You have to make sure that your dll is present as well as ANY dependency this dll has. This keypoint is usually where the headache kicks in.



回答5:

Using ProcessMonitor (from the Sysinternals Suite), you could see which DLL your process is looking for right before the exception is thrown.



回答6:

The dependency walker statically resolves all the DLLs needed by a native PE file and flags missing dependencies, while Fusion Log Viewer catches assembly binding problems in managed code during runtime. For managed .Net code that loads native DLLs dynamically these tools are not enough.

Here is a blog post on how you can use Process Monitor to debug “Could not load file or assembly” problems: https://www.codeproject.com/Articles/560816/Troubleshooting-dependency-resolution-problems-usi