Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my .NET App does not load an assembly located in a folder added to the PATH environment variable?

Tags:

c#

.net

dll

I have a .Net application which uses a couple of .Net dll's, these dll's are located in a folder which is included in the PATH environment variable, but when I start my .Net App if fails with the error:

Could not load file or assembly 'FxDoc.dll, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d4261664821' or one of its dependencies. The system cannot find the file specified.

I already have read these MSDN entries Search Path Used by Windows to Locate a DLL and Dynamic-Link Library Search Order

Which states:

Windows then searches for the DLLs in the following sequence:

The directory where the executable module for the current process is located.

The current directory.

The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.

The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.

The directories listed in the PATH environment variable.

My questions are:

  1. What factors could be causing this error?
  2. Is the above information valid for .Net?
like image 796
Salvador Avatar asked Sep 08 '11 00:09

Salvador


1 Answers

The rules for .NET assemblies and plain-old DLLs are not the same. The rules you listed are for plain-old DLLs.

Assembly loading is surprisingly complicated, but the basic search order is like this:

  1. Global Assembly Cache (GAC)
  2. A long list of other locations (including the application base directory, and various subdirectories depending on culture, etc...)

The environment variable PATH isn't used for .NET assemblies. For all the gruesome details, you'll probably want to look at the official documentation:

http://msdn.microsoft.com/en-us/library/aa720133.aspx

Most places I've worked, people stick to the basics -- either the assembly goes into the GAC, or it's placed in the same directory as the application.

You might also be interested in this tool (fuslogvw.exe) which helps you figure out why the loader isn't finding your assembly:

http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

like image 167
JohnD Avatar answered Sep 19 '22 13:09

JohnD